Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to setModel for QListWidget

Tags:

python

pyqt

Is there any way to setModel for QListWidget? I am getting AttributeError: QListWidget.setModel is a private method on this:

class Model(QtCore.QAbstractListModel):
    def __init__(self):
        QtCore.QAbstractListModel.__init__(self)
        self.items=[]
    def rowCount(self, parent=QtCore.QModelIndex()):
        return len(self.items)
    def flags(self,index):
        return QtCore.Qt.ItemIsEditable

view=QtGui.QListWidget()

viewModel=Model()
view.setModel(viewModel)
like image 297
alphanumeric Avatar asked Sep 16 '14 19:09

alphanumeric


People also ask

What is QListWidget?

QListWidget is a convenience class that provides a list view similar to the one supplied by QListView , but with a classic item-based interface for adding and removing items. QListWidget uses an internal model to manage each QListWidgetItem in the list.


2 Answers

I don't think so you can set model for QListWidget. Because QListWidget has its own model. But you can use QListView and you can set your own model to QListView

like image 198
Achayan Avatar answered Oct 23 '22 22:10

Achayan


I simply would like to complete @Achayan's answer: if you have a look at Qt's documentation, you can find a very good tutorial explaining precisely the difference between

  • implementation via standard widgets
  • implementation via Model/View

In the first case, the standard widgets own a version of the data, which can seem easier to do and manage at first glance, but can lead to problem of synchronization. Indeed, if the widget itself (which is a view) owns its own version of the data, you have to be sure that this data is synchronized with the database. And what's more, if you have, let's say 2 or 3 others widgets of the same model (example: a table, a list and a combobox representing the same entity), therefore you have four versions of the data...

The second one is better if you want a more decoupled and flexible implementation: the model is really "simply" a view, and does not own any data. He simply interacts with the model, which therefore has to implement a given interface (QAbstractItemModel).

In your case: QListWidget is the "standard widget", and QListView is the view of the Model/View implementation.

like image 32
Edouard Berthe Avatar answered Oct 23 '22 22:10

Edouard Berthe