Search code examples
qtqwidget

How to implement a Clickable Widget for QListView?


I want to implement my own Widgets for a QListView. Like this: enter image description here If i click on this widget i want to do something.

In time, I only have experience with the QML-Version of the ListView. Can someone explain how to insert this widget to the a QListView?

Greetings

UPDATE

In my Project i want a GUI like this: enter image description here In my first ListView I want to show items, that has a ListView, too. The text of each item can to be update.


Solution

  • There are 2 ways:

    1. Set custom widget for each index: QAbstractItemView::setIndexWidget. Note: there may be problems with interaction with widgets. This way is typically used only for displaying static content.
    2. Create custom QStyledItemDelegate and override editorEvent method. See model-view programming for details.

    Update: I propose you next design:

    1. Create widget with image list + "dynamic content" + labels
    2. Create ScrollArea with vertical layout and add there widgets (1.)

    (2.) is prefferable than simple listview, because listview doesn't design for such cases. Listview designed for showing some data, but not to be a container for other complex widgets.

    Pros: you will have fully interactable widgets.

    Cons: you need to code a bit ;)