[PyQt] Best way to add combobox into table?

Ke Gao ke.gao.ut at gmail.com
Wed May 30 00:53:41 BST 2018


Thanks Maurizio. The problem solved by using QTableView.

Previously, I used exactly the same way as your code. The
self.table.cellWidget(4,
0) returns a QWidget, rather than QComboBox. I'm using Anaconda python 3
with PyQt 5.6. I guess this is a bug of either Anaconda PyQt building or
PyQt 5.6 itself. When I insert QSpinBox into QTableWidget, it works well
and returns QSpinBox when using self.table.cellWidget(4, 0).




On Tue, May 29, 2018 at 4:00 PM, Maurizio Berti <maurizio.berti at gmail.com>
wrote:

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



-- 
..............................................................................................................................................
Ke Gao
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20180529/44e45d08/attachment-0001.html>


More information about the PyQt mailing list