Skip to content
Advertisement

Appending a row with widgets and text to a QStandardItemModel

I want to append a QWidget to a QStandardItemModel in a QTableView

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))

But when I try this the rows appear, event the it gets displayed, but never the Widgets I use in

Advertisement

Answer

Adding a widget to the data does not show the widget, it only stores it. If you want to display a widget then use the setIndexWidget method:

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)
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement