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

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



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


  • Lifetime Qt Champion

    @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


  • Qt Champions 2019



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


  • Lifetime Qt Champion

    @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



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


  • Lifetime Qt Champion

    @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



  • @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);
        }
    
    }```

  • Lifetime Qt Champion

    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.



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


Log in to reply