1

Please have a look at the picture. The bottom widget is a list widget. It works like this. When you click on insert button a new QListWidgetItem will be added dynamically in the bottom ListWidget. Now there are three items in the widget. Each item may contain CheckBoxes, ComboBoxes, lineEdit etc..

enter image description here

I need to get all those data from the listWidgetItem. Unfortunately, the listwidgetitem.text() is the only way I can retrieve data from the item. What to do then, to get all these data.

Brendan Abel
  • 35,343
  • 14
  • 88
  • 118
ameen
  • 13
  • 6

2 Answers2

1

You shouldn't (in general) use QListItemWidget for this, but instead a proper QAbstractItemModel-subclass. With that, you can back your listwidget with whatever data you want, and access that data.

There is a utility-class available already called QStandardItemModel which inherits from QAbstractItemModel, and should get you started fast.

Custom QStandardItemModel with custom data method

Community
  • 1
  • 1
deets
  • 6,285
  • 29
  • 28
  • Can you please post an example of the above solution, which contain only a CheckBox as an item member. The above example from the link throws error :( `PyQt4.QtCore.QVariant represents a mapped type and cannot be instantiated` – ameen Mar 24 '16 at 11:52
  • have you tried looking for other examples of QAbstractListModel? It is also very well documented. – deets Mar 24 '16 at 12:17
  • 1
    Omit the two occurrences of `QVariant()`, e.g. just return `self.listdata[index.row()]` – ngulam Mar 24 '16 at 12:22
  • It seems that's correct - just return a string, or probably None. PyQt changed it's API there. – deets Mar 24 '16 at 12:26
  • @deets ListModel is just one dimensional. isn't it? I need more than one widget in a single row. How can I do that using ListModel? – ameen Mar 24 '16 at 12:40
  • 1
    Have you read the documentation? it is not. QAbstractItemModel can even support full trees with several columns / row, and QListModel supports full table views. – deets Mar 24 '16 at 12:41
  • @deets. There's no such class as `QListModel`, so I assume you meant [QAbstractListModel](http://doc.qt.io/qt-4.8/qabstractlistmodel.html#details) - which most certainly is one-dimensional. – ekhumoro Mar 24 '16 at 18:29
  • @ekhumoro you are right. However, then the solution is to use QAbstractItemModel. – deets Mar 24 '16 at 18:30
1

You can store multiple pieces of arbitrary data on a QListWidgetItem (indeed, all the Item widgets support this). You need to define a custom role for your data.

Data1Role = QtCore.Qt.UserRole + 1
Data2Role = QtCore.Qt.UserRole + 2

item = QtGui.QListWidgetItem()
item.setData(Data1Role, 'Any data')
item.setData(Data2Role, 42)

print item.data(Data1Role)
# "Any data"

However, this data isn't going to be displayed anywhere on the QListWidgetItem. The only data that is displayed by default is the data set on the QtCore.Qt.DisplayRole (which is what text() and setText() use).

You are probably better off using a QTreeWidget, which supports multiple columns (for the multiple pieces of data) and creating a custom subclass of QTreeWidgetItem. If you want the user to be able to edit the data in the table, you're going to need to create a QItemDelegate as well to create the QLineEdits and QComboBoxes for editing the data.

Brendan Abel
  • 35,343
  • 14
  • 88
  • 118