Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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:

    @
    ui->listWidget->setSelectionMode(QAbstractItemView::SingleSelection);
    ui->listWidget->setCurrentRow(0);
    this->on_listWidget_clicked(ui->listWidget->currentIndex()); // Everything works here.
    ui->listWidget->currentItem()->setSelected(TRUE);
    ui->listWidget->currentItem()->isSelected(); // returns true here!
    ui->listWidget->show();
    @

    Please help me to find solution!

    [edit: added missing coding tags SGaist]


  • Lifetime Qt Champion

    Hi,

    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{
    
        Q_OBJECT
        
         public:
             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;
         private:
               Ui::MyObjectWidget *ui;
               QPointer<MyObject> myobj;
    }
    

    and in my .cpp:

    MyObjectWidget::MyObjectWidget(QPointer<MyObject> obj, QListWidget *parent, int type) :
        QWidget (parent),
        QListWidgetItem(parent,type),
        ui(new Ui::MyObjectWidget),
        myobj(obj)
    {
        ui->setupUi(this);
        this->ui->progressBar->setValue(getObj()->getIntVal());
        this->ui->lbl_name->setText(getMyObj()->getName());
    }
    
    
    QPointer<MyObject>MyObjectWidget::getObj()
    {
        return this->myobj;
    }
    
    QSize MyObjectWidget::sizeHint() const
    {
        return QWidget::sizeHint();
    }
    
    QVariant MyObjectWidget::data(int role) const
    {
        if(role==Qt::ItemDataRole::SizeHintRole){
            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
    {
    public:
        MyObjectListWidget(QWidget *parent=nullptr);
    
        bool addObj(QPointer<MyObjectWidget> object);
    
    private:
        QList<QPointer<MyObjectWidget>> widgets;
    };
    

    in .cpp :

    
    bool MyObjectListWidget::addObj(QPointer<MyObjectWidget> object)
    {
        addItem(object);
        qDebug()<<object->flags().testFlag(Qt::ItemFlag::ItemIsSelectable);
        widgets.push_back(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!
    Stupid,
    i totally forgot to remove previous style sheets on all the related widgets


  • Lifetime Qt Champion

    @Ebrahim-Karimi
    Hi
    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
    Design.

    Since you can use any widget with
    https://doc.qt.io/qt-5/qlistwidget.html#setItemWidget
    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

    @Ebrahim-Karimi
    Hi
    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