将带有小部件和文本的行附加到 QStandardItemModel

Appending a row with widgets and text to a QStandardItemModel

我想在 QTableView

中将 QWidget 附加到 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)