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 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?

    eyllanescE SGaistS 2 Replies Last reply
    0
    • Christian EhrlicherC Christian Ehrlicher

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

        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?

        eyllanescE Offline
        eyllanescE Offline
        eyllanesc
        wrote on 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

          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?

          SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on 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
          0
          • SGaistS SGaist

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

            Christian EhrlicherC 1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on 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
              0
              • SGaistS SGaist

                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 last edited by
                #6

                @SGaist okay, any suggestions?

                1 Reply Last reply
                0
                • A akshaybabloo

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

                  Christian EhrlicherC Offline
                  Christian EhrlicherC Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on 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
                  0
                  • Christian EhrlicherC Christian Ehrlicher

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

                    • Login

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