'QStyledItemDelegate / QAbstractItemDelegate for QListView
My aim is to create something like contact app, where I can list contacts and choose it to see information about person. I figure out that one of the possible solution is to use QListView + QStyledItemDelegate / QAbstractItemDelegate. The information about it is very difficult so I don't understand it clearly;
(Contact should look something like https://www.sketchappsources.com/free-source/4395-ios-contacts-screen-app-sketch-freebie-resource.html)
So how should I use QAbstractItemDelegate ( I heard that I must reimplement paintEvent )?
Solution 1:[1]
I suggest you to start with a data model.
- Use
QStandardItemModelclass for beginning and populate it withQStandardItemclass instances. It would allow you to set icon, text, font, background, size and other properties for items. Refer to https://doc.qt.io/qt-5/qstandarditemmodel.html#details - Set your model to
QListViewusingsetModel - To handle items clicked connect to
QListView'sclickedsignal.
To render items in more complex way you should
- Override
QStyledItemDelegateclass and it'spaintandsizeHintmethods. In thepaintmethod you should implement rendering and yoursizeHintmethod should return a valid size for items. Refer to https://doc.qt.io/qt-5/qabstractitemdelegate.html#details - To get item data to render use
datamethod ofQModelIndexreference that is passed topaintmethod. Use different roles to get appropriate data. Refer to https://doc.qt.io/qt-5/qt.html#ItemDataRole-enum - Use your delegate class by setting it to
QListViewbysetItemDelegate.
Model should be set to QListView and item clicks are handled in same way.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Alexey |
