Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QListWidget doesn't highlights selected item :(

QListWidget doesn't highlights selected item :(

Scheduled Pinned Locked Moved General and Desktop
12 Posts 5 Posters 2.6k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Stramonium
    wrote on last edited by
    #1

    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]

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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 ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      E 1 Reply Last reply
      0
      • E Offline
        E Offline
        Ebrahim Karimi
        wrote on last edited by Ebrahim Karimi
        #3

        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?

        Standing on the shoulder of the giants

        1 Reply Last reply
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

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

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          E 1 Reply Last reply
          0
          • Christian EhrlicherC Christian Ehrlicher

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

            E Offline
            E Offline
            Ebrahim Karimi
            wrote on last edited by
            #5

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

            Standing on the shoulder of the giants

            1 Reply Last reply
            0
            • Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @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.

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              E 1 Reply Last reply
              2
              • Christian EhrlicherC Christian Ehrlicher

                @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.

                E Offline
                E Offline
                Ebrahim Karimi
                wrote on last edited by
                #7

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

                Standing on the shoulder of the giants

                1 Reply Last reply
                0
                • SGaistS SGaist

                  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 ?

                  E Offline
                  E Offline
                  Ebrahim Karimi
                  wrote on last edited by
                  #8

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

                  Standing on the shoulder of the giants

                  1 Reply Last reply
                  0
                  • E Offline
                    E Offline
                    Ebrahim Karimi
                    wrote on last edited by
                    #9

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

                    Standing on the shoulder of the giants

                    mrjjM 1 Reply Last reply
                    1
                    • E Ebrahim Karimi

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

                      mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      @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.

                      E 1 Reply Last reply
                      2
                      • mrjjM mrjj

                        @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.

                        E Offline
                        E Offline
                        Ebrahim Karimi
                        wrote on last edited by
                        #11

                        @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

                        Standing on the shoulder of the giants

                        mrjjM 1 Reply Last reply
                        0
                        • E Ebrahim Karimi

                          @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

                          mrjjM Offline
                          mrjjM Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

                          @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.

                          1 Reply Last reply
                          0

                          • Login

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved