Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. General talk
  3. Qt 6
  4. Find QListWidgetItem by custom widget object name
QtWS25 Last Chance

Find QListWidgetItem by custom widget object name

Scheduled Pinned Locked Moved Solved Qt 6
qlistwidgetitem
8 Posts 4 Posters 2.5k Views
  • 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.
  • A Offline
    A Offline
    akshaybabloo
    wrote on 29 Dec 2020, 01:28 last edited by
    #1

    I am trying to find a widget item by its object name. I was able to find it by text using findItems, but as I am using a custom widget the text superimposes on the widget:

    Screen Shot 2020-12-29 at 2.23.34 PM.png

    I tried setting the pixel size to 1, but it looks like there is a dot on the list item.

    The cross button from the above image is connected to the parents method. I was able to send the widgets object name by doing - emit sendMessage(this->d_ptr->q_ptr->objectName());, but for this I have to set the text of the item in the parent.

    Is there a better way to find the widget?

    E S 2 Replies Last reply 29 Dec 2020, 02:43
    0
    • C Christian Ehrlicher
      31 Dec 2020, 14:47

      @akshaybabloo said in Find QListWidgetItem by custom widget object name:

      The only way to findItem in QListWidget is to set the item text, but here I am using custom widget

      Then you have to iterate over the items by yourself. Shouldn't be too hard to write a simple for loop.

      A Offline
      A Offline
      akshaybabloo
      wrote on 2 Jan 2021, 06:53 last edited by
      #8

      @Christian-Ehrlicher True and this is what I did. It works:

      void MainWindow::removeItem(const QString &text) {
      
          for (int i = 0; i < ui->listWidget->count(); ++i) {
              auto item = ui->listWidget->item(i);
              auto itemWidget = dynamic_cast<CustomWidget*>(ui->listWidget->itemWidget(item));
              if (itemWidget->getText() == text){
                  delete item;
                  break;
              }
          }
      }
      
      1 Reply Last reply
      0
      • A akshaybabloo
        29 Dec 2020, 01:28

        I am trying to find a widget item by its object name. I was able to find it by text using findItems, but as I am using a custom widget the text superimposes on the widget:

        Screen Shot 2020-12-29 at 2.23.34 PM.png

        I tried setting the pixel size to 1, but it looks like there is a dot on the list item.

        The cross button from the above image is connected to the parents method. I was able to send the widgets object name by doing - emit sendMessage(this->d_ptr->q_ptr->objectName());, but for this I have to set the text of the item in the parent.

        Is there a better way to find the widget?

        E Offline
        E Offline
        eyllanesc
        wrote on 29 Dec 2020, 02:43 last edited by
        #2

        @akshaybabloo please provide a minimal workable example

        If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

        1 Reply Last reply
        0
        • A akshaybabloo
          29 Dec 2020, 01:28

          I am trying to find a widget item by its object name. I was able to find it by text using findItems, but as I am using a custom widget the text superimposes on the widget:

          Screen Shot 2020-12-29 at 2.23.34 PM.png

          I tried setting the pixel size to 1, but it looks like there is a dot on the list item.

          The cross button from the above image is connected to the parents method. I was able to send the widgets object name by doing - emit sendMessage(this->d_ptr->q_ptr->objectName());, but for this I have to set the text of the item in the parent.

          Is there a better way to find the widget?

          S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 29 Dec 2020, 19:42 last edited by
          #3

          Hi,

          @akshaybabloo said in Find QListWidgetItem by custom widget object name:

          emit sendMessage(this->d_ptr->q_ptr->objectName());

          This looks a bit nasty and from the looks of it could be replaced by:

          emit sendMessage(objectName());
          

          How do you get the text to draw like that ? The object name is usually not visible at all.

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

          A 1 Reply Last reply 31 Dec 2020, 04:10
          0
          • S SGaist
            29 Dec 2020, 19:42

            Hi,

            @akshaybabloo said in Find QListWidgetItem by custom widget object name:

            emit sendMessage(this->d_ptr->q_ptr->objectName());

            This looks a bit nasty and from the looks of it could be replaced by:

            emit sendMessage(objectName());
            

            How do you get the text to draw like that ? The object name is usually not visible at all.

            A Offline
            A Offline
            akshaybabloo
            wrote on 31 Dec 2020, 04:10 last edited by
            #4

            @SGaist I use setObjectName when adding the widget to the list.

            @SGaist and @eyllanesc the example code can be found at https://github.com/akshaybabloo/qlistwidget-custom-widget.

            The only way to findItem in QListWidget is to set the item text, but here I am using custom widget. So, I had to set the item text make the font size to 1, but still, you can see the tiny text to the left.

            Screen Shot 2020-12-31 at 5.09.36 PM.png

            It would be nice to find the list item by its object name instead on the item text.

            C 1 Reply Last reply 31 Dec 2020, 14:47
            0
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 31 Dec 2020, 08:57 last edited by
              #5

              Well... You are putting a text in your item and set a widget on it which does not make much sense.

              In any case, using setItemWidget like you do is going to turn to a bad idea. It's not meant to be used to fill QListWidget.

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

              A 1 Reply Last reply 31 Dec 2020, 14:31
              0
              • S SGaist
                31 Dec 2020, 08:57

                Well... You are putting a text in your item and set a widget on it which does not make much sense.

                In any case, using setItemWidget like you do is going to turn to a bad idea. It's not meant to be used to fill QListWidget.

                A Offline
                A Offline
                akshaybabloo
                wrote on 31 Dec 2020, 14:31 last edited by
                #6

                @SGaist okay, any suggestions?

                1 Reply Last reply
                0
                • A akshaybabloo
                  31 Dec 2020, 04:10

                  @SGaist I use setObjectName when adding the widget to the list.

                  @SGaist and @eyllanesc the example code can be found at https://github.com/akshaybabloo/qlistwidget-custom-widget.

                  The only way to findItem in QListWidget is to set the item text, but here I am using custom widget. So, I had to set the item text make the font size to 1, but still, you can see the tiny text to the left.

                  Screen Shot 2020-12-31 at 5.09.36 PM.png

                  It would be nice to find the list item by its object name instead on the item text.

                  C Offline
                  C Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on 31 Dec 2020, 14:47 last edited by
                  #7

                  @akshaybabloo said in Find QListWidgetItem by custom widget object name:

                  The only way to findItem in QListWidget is to set the item text, but here I am using custom widget

                  Then you have to iterate over the items by yourself. Shouldn't be too hard to write a simple for loop.

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

                  A 1 Reply Last reply 2 Jan 2021, 06:53
                  0
                  • C Christian Ehrlicher
                    31 Dec 2020, 14:47

                    @akshaybabloo said in Find QListWidgetItem by custom widget object name:

                    The only way to findItem in QListWidget is to set the item text, but here I am using custom widget

                    Then you have to iterate over the items by yourself. Shouldn't be too hard to write a simple for loop.

                    A Offline
                    A Offline
                    akshaybabloo
                    wrote on 2 Jan 2021, 06:53 last edited by
                    #8

                    @Christian-Ehrlicher True and this is what I did. It works:

                    void MainWindow::removeItem(const QString &text) {
                    
                        for (int i = 0; i < ui->listWidget->count(); ++i) {
                            auto item = ui->listWidget->item(i);
                            auto itemWidget = dynamic_cast<CustomWidget*>(ui->listWidget->itemWidget(item));
                            if (itemWidget->getText() == text){
                                delete item;
                                break;
                            }
                        }
                    }
                    
                    1 Reply Last reply
                    0

                    6/8

                    31 Dec 2020, 14:31

                    • Login

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