[SOLVED] QListView - make a list of items that are collections of widgets?
If someone could just point me in the right direction, that'd be great.
I want to make a scrollable, drag-and-dropable list of things. Each thing is a collection of widgets (label, check box, some other options.) I've looked at QAbstractListModel, QAbstractItemView, QStyledItemDelegate and other but I'm confused as to the right way to approach this problem.
Could someone point me to a help page or an example on how to use a QListView as a container for containers of widgets?
Edit: I’m writing from scratch so I’m open to any design. I’d suspect that there is a “Qt way” and I want to learn that.
Here’s a visual example: https://dl.dropboxusercontent.com/u/9258942/mockup.png
The normal QtListView is the image on the left – Normal ListBox.
The list I need to create is on the right – Custom ListBox.
From a design point of view you should create a custom model based on QAbstractListModel that will interact with your list of "thing".
For more ideas, you should give some information about your container like how are you storing that container ?
I'm writing from scratch so I'm open to any design. I'd suspect that there is a "Qt way" and I want to learn that.
Here's a visual example: https://dl.dropboxusercontent.com/u/9258942/mockup.png
The normal QtListView is the image on the left - Normal ListBox.
The list I need to create is on the right - Custom ListBox.
Should that only be an image of the widget collection of an element or should it also be usable in-place ?
SOLVED: "setItemWidget":http://doc.qt.io/qt-5/qlistwidget.html#setItemWidget is what I was looking for.
Here's the code (comments/corrections/improvements welcome!):
@ lw = QListWidget()
for var in range( 0, 10 ): w = QGroupBox() vbl = QVBoxLayout( w ) # Row 1 ---------- fm = QFrame() #fm.setFrameStyle( QFrame.Panel ) hz1 = QHBoxLayout( fm ) hz1.setSizeConstraint( QLayout.SetFixedSize ) lbl1 = QLabel( "Item %d" % var ) spn = QSpinBox() spn.setValue( 3 ) hz1.addWidget( lbl1 ) hz1.addWidget( spn ) # end Row 1---------- # Row 2 ---------- fm2 = QFrame() #fm2.setFrameStyle( QFrame.Panel ) hz2 = QHBoxLayout( fm2 ) hz2.setSizeConstraint( QLayout.SetFixedSize ) cb = QCheckBox( "Enabled") cmb = QComboBox() cmb.addItem( "Choice" ) hz2.addWidget( cb ) hz2.addWidget( cmb ) # end Row 2---------- vbl.addWidget( fm ) vbl.addWidget( fm2 ) vbl.setSizeConstraint( QLayout.SetFixedSize ) lwi = QListWidgetItem() lwi.setSizeHint( vbl.sizeHint() ) lw.addItem( lwi ) lw.setItemWidget( lwi, w ) lw.show()
Beware, setItemWidget comes with a performance cost especially if you have many widgets in there.
What is your software purpose ?
@SGaist, thanks for the heads up. I'm still pretty green to the Qt ways (but learning quickly.)
I'm essentially creating a "pipes and filters pattern":http://blog.petersobot.com/pipes-and-filters. Each filter will be represented by the entries in the list box so that each filter can be individually enabled and configured.
There won't be more than, say, 10 filters which will be used to initialize the list only once at startup. It'll only be running on non-embedded systems (desktop variants) so hopefully performance won't be an issue.
Will you be adding filters dynamically ?
Filters will only be instantiated and then added at startup per the current design.
Why do you ask?
Because you could be writing a drag & drop editor where you could be adding/removing filters dynamically and that could influence the design :)