Qt World Summit: Submit your Presentation

QListWidget doesn't highlights selected item :(

  • Hello!

    When I select item or row, everything works fine, but the item remains not highlighted. Abstract from code:

    this->on_listWidget_clicked(ui->listWidget->currentIndex()); // Everything works here.
    ui->listWidget->currentItem()->isSelected(); // returns true here!

    Please help me to find solution!

    [edit: added missing coding tags SGaist]

  • Lifetime Qt Champion


    You should at least add which version of Qt you are using and which OS you are running. Also how are you populating your QListWidget ?

  • I have the same issue
    I'm trying to build a cross platform application so appreciate any suggestion or recommendation about either code style or any OS specific details
    My OS is Ubuntu 19.04
    QT version 5.13
    I created a custom widget class:

    class MyObjectWidget : public QWidget,public QListWidgetItem{
             explicit MyObjectWidget(QPointer<MyObject> obj,QListWidget *parent = nullptr,int type=0);
                   QSize sizeHint()const override;
              QPointer<MyObject> getObj();
                  QVariant data(int role=Qt::ItemDataRole::DisplayRole)const override;
               Ui::MyObjectWidget *ui;
               QPointer<MyObject> myobj;

    and in my .cpp:

    MyObjectWidget::MyObjectWidget(QPointer<MyObject> obj, QListWidget *parent, int type) :
        QWidget (parent),
        ui(new Ui::MyObjectWidget),
        return this->myobj;
    QSize MyObjectWidget::sizeHint() const
        return QWidget::sizeHint();
    QVariant MyObjectWidget::data(int role) const
            return QWidget::sizeHint();
        }else if(role == Qt::ItemDataRole::BackgroundRole){
            return QVariant(palette().highlight()); // even made sure :(
        return QVariant();

    and for population i have a custom QListWidget:

    class MyObjectListWidget : public QListWidget
        MyObjectListWidget(QWidget *parent=nullptr);
        bool addObj(QPointer<MyObjectWidget> object);
        QList<QPointer<MyObjectWidget>> widgets;

    in .cpp :

    bool MyObjectListWidget::addObj(QPointer<MyObjectWidget> object)
        return true;

    it's a little tricky this way but i believe its possible to make it work, right?

  • Lifetime Qt Champion

    Why do you derive from QListWidgetItem and QWidget? Why do you think this is needed and what do you gain?

  • @Christian-Ehrlicher i wanted to use qt designer to generate the UI so i thought i would be a good solution, any suggestion?

  • Lifetime Qt Champion

    @Ebrahim-Karimi said in QListWidget doesn't highlights selected item :(:

    i wanted to use qt designer to generate the UI so i thought i would be a good solution, any suggestion?

    I don't see any relation between designer and your code above, sorry.

  • @Christian-Ehrlicher its ok bro but this is killing me how can i solve the highlight thing? :'(

  • @SGaist I believe you are a QT Ninja, can you help me with this?

  • OMG!
    i totally forgot to remove previous style sheets on all the related widgets

  • Lifetime Qt Champion

    Glad you found the issue.
    It's one of the main reasons that many use just 1 stylesheet we set on Application
    so it affects all widgets in the app.

    Im not sure i understand the
    class MyObjectWidget : public QWidget,public QListWidgetItem

    Since you can use any widget with
    to set on any item - so I would just make them as completely normal widgets with
    .h/.cpp and UI file and then use this like that.

  • @mrjj please correct me if i'm wrong
    if i don't inherit QListWidgetItem i have to create another raw QListWidgetItem and add it to the QListWidget then setItemWidget(QListWidgetItem *rawItem, QWidget *myCustomWidget) right?
    so i decided instead of creating two objects, i can inherit both in one class and create only one object and use that one for both addItem and setItemWidget methods

  • Lifetime Qt Champion

    Yes, that is the normal way. Also so the Widget is owned and handled by
    the QListWidget like its items.

    I understand your rationale for then combining them into one class but
    if you then reuse the Widgets in another context, they are also half a QListWidgetItem and if you later wanted to use the View version of the List widget with a custom model, then the QListWidgetItem part is also not needed.

    Overall, in OOP we use inheritance when we have something we can say "is a"
    and i think what i find odd with it - is that we say the QListWidgetItem is now a QWidget which means it can no longer be copied and radically changed the
    expected contract of items. Like you could new a MyObjectWidget and place it on a form.

    But I dont think any part of Qt, include proxies will try to copy QListWidgetItems, so I dont think it will come back and haunt you :)

    So my comment was more out of o.O surprise of such a combination than
    out of fear or experience - that it will burn💥 later.

Log in to reply