How do i search from items of list widget ??,
-
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..
-
@Mr-Workalot
Hi
The findItems returns a list
so you can loop then and set background to make them standoutQString search("take"); QList<QListWidgetItem *> list = ui->listWidget->findItems("take", Qt::MatchContains); for ( QListWidgetItem *item : list ) item->setBackgroundColor(Qt::red);
-
-
Thanks, This narrows my search alot, but can you please provide code segments, im really stuck on this.
-
@Mr-Workalot
Hi
The findItems returns a list
so you can loop then and set background to make them standoutQString search("take"); QList<QListWidgetItem *> list = ui->listWidget->findItems("take", Qt::MatchContains); for ( QListWidgetItem *item : list ) item->setBackgroundColor(Qt::red);
-
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 ??
-
@Mr-Workalot
Hi
To clear any "reds" you can usefor(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); } }```
-
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.