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. How do i search from items of list widget ??,
Forum Update on Monday, May 27th 2025

How do i search from items of list widget ??,

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 4 Posters 4.1k 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.
  • M Offline
    M Offline
    Mr-Workalot
    wrote on last edited by Mr-Workalot
    #1

    0cb2b72a-d62a-4eab-8d6b-29900b0f0bcb-image.png

    as seen in the image above my QListWIdget is populated with items, i want to take input from Qlineedit which is "Guide" in this case and when i click search Qpushbutton, the QlistWidget should show that items which contain text "Guide ", maybe highlight them.

    Any idea how to accomplish this.

    Thanks this forum is relly helpful..

    1 Reply Last reply
    0
    • M Mr-Workalot

      Thanks, This narrows my search alot, but can you please provide code segments, im really stuck on this.

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

      @Mr-Workalot
      Hi
      The findItems returns a list
      so you can loop then and set background to make them standout

          QString search("take");
          QList<QListWidgetItem *> list = ui->listWidget->findItems("take", Qt::MatchContains);
          for ( QListWidgetItem *item : list )
              item->setBackgroundColor(Qt::red);
      
      

      alt text

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

        See QListWidget::findItems()

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

        1 Reply Last reply
        2
        • M Offline
          M Offline
          Mr-Workalot
          wrote on last edited by
          #3

          Thanks, This narrows my search alot, but can you please provide code segments, im really stuck on this.

          mrjjM 1 Reply Last reply
          0
          • M Mr-Workalot

            Thanks, This narrows my search alot, but can you please provide code segments, im really stuck on this.

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

            @Mr-Workalot
            Hi
            The findItems returns a list
            so you can loop then and set background to make them standout

                QString search("take");
                QList<QListWidgetItem *> list = ui->listWidget->findItems("take", Qt::MatchContains);
                for ( QListWidgetItem *item : list )
                    item->setBackgroundColor(Qt::red);
            
            

            alt text

            1 Reply Last reply
            2
            • M Offline
              M Offline
              Mr-Workalot
              wrote on last edited by
              #5

              Thanks so much , This works great, However i am looking for a slight improvement,
              like when ever i search "Take" all items with 'take' turn red as expected, but when i change my search to "New Item" in your example, the items with 'new' are changed to red but also the old items which were previously searched are still red.

              any way to deal with this ???, maybe can you show me how to take all items and store it in a list , then i can repopulate the Qlistwidget with the list containing all original items unhighlighted in red ??

              mrjjM 1 Reply Last reply
              0
              • M Mr-Workalot

                Thanks so much , This works great, However i am looking for a slight improvement,
                like when ever i search "Take" all items with 'take' turn red as expected, but when i change my search to "New Item" in your example, the items with 'new' are changed to red but also the old items which were previously searched are still red.

                any way to deal with this ???, maybe can you show me how to take all items and store it in a list , then i can repopulate the Qlistwidget with the list containing all original items unhighlighted in red ??

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

                @Mr-Workalot
                Hi
                To clear any "reds" you can use

                for(int i = 0; i < listWidget->count(); ++i)
                {
                    QListWidgetItem* item = listWidget->item(i);
                    item->setBackgroundColor(Qt::white); 
                }
                

                This assumes you do not have that many items. (like 1000 )
                as then keeping track of the "reds" would be more efficient.

                ps.
                setBackgroundColor is obsolete, please use setBackground. my bad. thx johnB

                M 1 Reply Last reply
                2
                • mrjjM mrjj

                  @Mr-Workalot
                  Hi
                  To clear any "reds" you can use

                  for(int i = 0; i < listWidget->count(); ++i)
                  {
                      QListWidgetItem* item = listWidget->item(i);
                      item->setBackgroundColor(Qt::white); 
                  }
                  

                  This assumes you do not have that many items. (like 1000 )
                  as then keeping track of the "reds" would be more efficient.

                  ps.
                  setBackgroundColor is obsolete, please use setBackground. my bad. thx johnB

                  M Offline
                  M Offline
                  Mr-Workalot
                  wrote on last edited by
                  #7

                  @mrjj Thanks for te reply, This looks like it should work perfectly, but i dunno why, my UI crashes when i have this, this is my code segment.

                  void MainWindow::on_clear_button_clicked()
                  {
                      for (int i = 0; i <= ui->elem_list->count();++i)
                      {
                          QListWidgetItem* item = ui->elem_list->item(i);
                          item->setBackgroundColor(Qt::white);
                      }
                  
                  }```
                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #8

                    Hi,

                    You have an off by one error.
                    Let's say count is 10, with your loop, you are going from 0 to 10, so you are accessing an eleventh item that does not exist.

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

                    M 1 Reply Last reply
                    2
                    • SGaistS SGaist

                      Hi,

                      You have an off by one error.
                      Let's say count is 10, with your loop, you are going from 0 to 10, so you are accessing an eleventh item that does not exist.

                      M Offline
                      M Offline
                      Mr-Workalot
                      wrote on last edited by
                      #9

                      @SGaist OMG.... How silly of me , Thanks a Lot for help QT Champions.

                      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