我想将QWidget
附加到QTableView
中的QStandardItemModel
self.table = QTableView()
self.ui.scrollArea.setWidget(self.table) #Not important but I left it in in case it had something to do with this question
self.model = QStandardItemModel()
self.table.setSelectionMode(QAbstractItemView.ExtendedSelection)
self.table.setModel(self.model)
...
@Slot(None)
def OpenFile(self):
dialog = QFileDialog(self)
dialog.setFileMode(QFileDialog.ExistingFiles)
if dialog.exec():
filenames = dialog.selectedFiles()
for i in range(len(filenames)):
it = QStandardItem()
it.setText(filenames[i].split("/")[-1])
self.samplespinbox = QDoubleSpinBox()
self.formatdropdown = QComboBox()
samplespinboxholder = QStandardItem()
samplespinboxholder.setData(self.samplespinbox)
formatdropdownholder = QStandardItem()
formatdropdownholder.setData(self.samplespinbox)
self.model.appendRow((it ,samplespinboxholder, formatdropdownholder))
但是当我尝试这样做时,出现了行,事件显示it
,但从未显示我在
向数据中添加小部件并不显示该小部件,而只是存储它。如果你想显示一个小部件,那么使用setIndexWidget方法:
for filename in filenames:
samplespinbox = QDoubleSpinBox()
formatdropdown = QComboBox()
it1 = QStandardItem(filename.split("/")[-1])
it2 = QStandardItem()
it3 = QStandardItem()
self.model.appendRow((it1 ,it2, it3))
self.table.setIndexWidget(it2.index(), samplespinbox)
self.table.setIndexWidget(it3.index(), formatdropdown)