I'm writing a PyQt application and am having some trouble creating a custom list view. I'd like the list to contain arbitrary widgets (one custom widget in particular). How would I go about this?
It seems that the alternative would be to create a table or grid view wrapped in a scrollbar. However, I'd like to be able to take advantage of the model/view approach as well as the nesting (tree-view) support the built-ins handle.
To clarify, the custom widgets are interactive (contain buttons), so the solution requires more than painting a widget.
I think you need to subclass QItemDelegate.
This code is taken from Qt's examples, the torrent application.
Basically as you can see it checks if the column to be painted is of a specific index, if so it paints a progress bar. I think you could tweak it a little and instead of using a QStyleOption you could use your own widget.
edit: don't forget to setup your item delegate with your QListView using setItemDelegate.
While investigating your question I've stumbled upon this thread, which elaborates how to paint a custom widget using a QItemDelegate, I believe it has all the info you might need.
another way you can add custom items in listWidget. for this purpose you need to add new class. name it as you want i just give it "myList" i hope you know how to add new items in project. :)
then in this new frame add your control as many as u want. like QLabels,QlineEdit etc
then in main class u have to add a listWidget name list or as you want and the write the following code
latter u can also change items properties using signals/slots ..
hope its help you ..!
@Idan's answer works well, but I'll post a simpler example I came up with. This item delegate just draws a black rectangle for each item.
And then you just need to set it for the list widget:
Assist says, that:
Sets the given widget to be displayed in the cell in the given row and column, passing the ownership of the widget to the table. If cell widget A is replaced with cell widget B, cell widget A will be deleted.
And there is analogs to this method in the most of QAbstractItemView descendants.
You have to subclass Q***Delegate only when you want editor widget to appear in View only when you hit EditTrigger, then vanish and let delegate render the view item in some way.
If I correct, you wanted to see control in item view all the time and be able to hit controls without the need to enter editing mode and wait while delegate creates editor and set its state, so you do not need to make specific delegate, just set widget into the view's item.
If I understand your question correctly, you want something like this:
where each row contains a custom set of widgets.
To achieve this, two steps.
Implement the row with a custom Widget
First, implement a custom widget that contains all the widgets needed per list row.
Here I am using a label and two buttons per row, with an horizontal layout.
Add rows to the list
Instanciating multiple rows is then just a matter of creating a widget item, and associate the custom widget to the item's row.