Important: Please read the Qt Code of Conduct -

QListWidget and items with several informations

  • Hello community!

    I'm programming a software for an American Football team where they can manage their formations and plays. It draws everything with QPainter into a self-programmed render widget and it can use it to create PNGs, JPEGs etc.

    For a good looking UI I heard of QListWidget and QListWidgetItem to show a list of the saved formations and plays. The icons you can add in QListWidgetItem are useful to show a little picture of the play so that you know immediately what play it is. The list itself only shows small picture and formation/play's name.

    The problem:
    According to "QListWidgetItem Reference": /qlistwidgetitem.html#setData you can only store one information which is a QVariant. But as informations I need the following:

    • Players (Positions in x and y and if Player is QB, RB, LB etc.)
    • Players' routes
    • Formation/Play's name
    • Formation/Play's notes
    • Play style (offense, defense, special)
    • Picture of formation/play (rendered with QPainter)

    When I double-click the item or single-click it and press a button the play should be displayed in my render-widget which also contains the above informations.

    Is there a way to save all these informations in QListWidgetItem? I'd like to avoid synchronizing QStringList in QListWidget and QList<Formation*>

    lg enforcer

  • Lifetime Qt Champion


    Put all these information in a class or structure and then put that in the item's data


    Have a look at the "QVariant": doc as well as the QMetaType

    Hope it helps

  • I'm not that familiar with QMetaType. I remember one project in Java where I updated a ListWidget each time a Vector<...> changed content (add object, delete object, update informations of object). I hoped to have it easier in Qt, but I think this is the most convenient way for me.

    But thanks for your advise.

  • Lifetime Qt Champion

    Then you could use a QListView with a custom model. Change the data in the model and the view will be updated for you accordingly.

  • How about an own class derived from QListWidgetItem. It inherits a pointer to my class where all needed infos are saved. It also consists of a slot connected with QListWidget::itemActivated(QListWidgetItem*) signal. If signal argument == this, then load info_pointer into my render-widget.
    In constructor I call setData(...) and setIcon(...).

  • Lifetime Qt Champion

    That's another solution, it's up to you to validate what's the best for you :)

Log in to reply