How to set Model for QListWidget

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

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

      

+3


source to share


2 answers


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



+4


source


I just would like to finish @ Achayan's answer: if you look at the Qt documentation you can find a very good tutorial explaining exactly the difference between

  • via standard widgets
  • using Model / View

In the first case, standard widgets own a version of the data , which may seem easier to work with and manage at a glance, but can lead to a synchronization problem>. Indeed, if the widget(which is a view) owns its own version of the data, you need to be sure that this data is in sync with the database. And what else if you have, say, 2 or 3 other widgets of the model of the same (example: table, list and dropdown representing the same object), so you have four versions of the data ...



The second option is better if you want a more decoupled and flexible implementation: the model is really "simple" and has no data. It just interacts with the model, so it must implement this interface ( QAbstractItemModel

).

In your case: QListWidget

is the "standard widget" and QListView

is the implementation representation of the Model / View.

+2


source







All Articles