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 )?
CodePudding user response:
I suggest you to start with a data model.
- Use
QStandardItemModel
class for beginning and populate it withQStandardItem
class 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
QListView
usingsetModel
- To handle items clicked connect to
QListView
'sclicked
signal.
To render items in more complex way you should
- Override
QStyledItemDelegate
class and it'spaint
andsizeHint
methods. In thepaint
method you should implement rendering and yoursizeHint
method 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
data
method ofQModelIndex
reference that is passed topaint
method. 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
QListView
bysetItemDelegate
.
Model should be set to QListView and item clicks are handled in same way.