[PyQt] Best way to add combobox into table?

Maurizio Berti maurizio.berti at gmail.com
Tue May 29 23:00:28 BST 2018


There are different approaches, depending on your needs and the size of the
table.
If the size is not very big, using QTableWidget is fine, and you can access
the data as you wrote (it works, see below).
While this has the advantage of mixing different kinds of widgets
independently (not row or column related), it also means that you should
track every combo, and adding or removing cells and widgets might be a bit
annoying for very large models.
The alternative is using a QStandardItemModel and set a custom item
delegate (QItemDelegate or, better, QStyledItemDelegate) for the column or
row you are interested into; if you want, you can set the default delegate
for the whole table too. In this way the table only cares about its data
(which is usually better) and you can set the combobox only when editing,
through the createEditor method of the delegate.
There is also the possibility of using
QAbstractItemDelegate.setIndexWidget(index, widget), which is very similar
to the use of the QTableWidget, but is not very suited for interaction.

I tried to use cellWidget to get the value from the combo and it works
without problems. Are you sure you implemented it in the right way?
Here's a simple example:

class Widget(QtWidgets.QWidget):
    def __init__(self):
        QtWidgets.QWidget.__init__(self)
        l = QtWidgets.QGridLayout()
        self.setLayout(l)
        self.table = QtWidgets.QTableWidget(20, 1)
        l.addWidget(self.table)
        for r in range(10):
            combo = QtWidgets.QComboBox()
            combo.addItems([str(i) for i in range(50)])
            self.table.setCellWidget(r, 0, combo)
        self.btn = QtWidgets.QPushButton()
        l.addWidget(self.btn)
        self.btn.clicked.connect(self.printValue)

    def printValue(self):
        print(self.table.cellWidget(4, 0).currentText())





2018-05-27 18:04 GMT+02:00 Ke Gao <ke.gao.ut at gmail.com>:

> Hi,
>
> I want to add a combobox into a a table, what is the best way, using
> tableview or tablewidget? I tried tablewidget and succeeded using
> setCellWidget. However, I can't obtain the current text in the combobox.
> For example, cellWidget(row, col).currentText(). Is there any way to read
> and change the data in the combobox in tablewidget?
>
> Thank you very much,
>
> Ke
>
> --
> ............................................................
> ............................................................
> ......................
> Ke Gao
>
> _______________________________________________
> PyQt mailing list    PyQt at riverbankcomputing.com
> https://www.riverbankcomputing.com/mailman/listinfo/pyqt
>



-- 
È difficile avere una convinzione precisa quando si parla delle ragioni del
cuore. - "Sostiene Pereira", Antonio Tabucchi
http://www.jidesk.net
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20180530/4fb0bc5a/attachment.html>


More information about the PyQt mailing list