Qt6 topics merged with General

List of Items with texts and icons. QAbstractItemModel?

  • Hi!
    I'm pretty new to QT, so there might be some misunderstandings in concepts.
    What I would like to display is a list (ListView) of Items of which each displays multiple texts and icons.

    I can successfully display single text elements with QStandardItemModel and I read about subclassing QAbstractItemModel if you need to display things differently. In QT help I also found an example for having a list of images (puzzle), but I couldn't find a combination or an example how to set the positions of multiple text elements.

    In the past in vc++ or bcc I usually used the graphical base class (for labels etc.), subclassed it and changed the paint and mouse[...] events until it looked like I want.

    To display a list of items that show multiple text and icons, would it be better to subclass QAbstractItemModel and put it into a Listview or to create my own "Listview" as a QFrame subclass?

    best Regards

  • The model does not determine how things are displayed, it just contains the data.

    The view determines how the data as a whole is displayed (tree, list, table, etc.)

    The delegate determines how individual items in the view are displayed.

    check http://doc.qt.io/qt-5/model-view-programming.html for more detail

    From your description it looks like what you need to subclass QStyledItemDelegate. What you need to do is store your text and icons in different Data Roles from Qt::UserRole onward and use the delegate paint method to retrieve them and display them.

    The star delegate example is a good place to start

  • @VRonin :
    Thanks for the fast and helpful reply!
    I took a look into the model/view explanation and the star delegate example.

    Edit: Sorry, I forgot to set the data to 0 on the QListView.
    For QListView setItem parameters are (data,role), for QTableWidget it's (role,data)

Log in to reply