1

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

Jakob
  • 15
  • 4
  • Note that setting instance attributes in loops (for instance, `self.samplespinbox = ...`) is completely pointless. Just use local variables, as you are already doing for `it`. – musicamante Aug 12 '21 at 07:16

1 Answers1

2

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)
eyllanesc
  • 235,170
  • 19
  • 170
  • 241