Searching bar in tree widget
-
I have the code below for searching items in tree widget. I doesnt search. When i finish editing, i cant see any item on widget.
It doesnt have any error. But when i debug, i saw that "arama" is emptyvoid MainWindow::on_ara_editingFinished() { QList<QTreeWidgetItem*> arama = ui->treeWidget2->findItems(ui->ara->text(), Qt::MatchContains, 0); for(QTreeWidgetItem* item : arama) { qDebug() << ui->ara->text(); ui->treeWidget2->setCurrentItem(item); } }
-
@suslucoder What does ui->ara->text() return? In which column do you want to search? Is there at least one item in the tree widget in the column you're searching containing the text ui->ara->text() returns?
-
@jsulm
I've changed my code toQList<QTreeWidgetItem*> arama = ui->treeWidget2->findItems(ui->ara->text(), Qt::MatchContains|Qt::MatchRecursive, 0); for(QTreeWidgetItem* item : arama) { ui->treeWidget2->setCurrentItem(item, 0); // item->setBackground(0, Qt::red); }
but setCurrentItem is set one item..of course...
But i want to see every item that contains the given text
-
@suslucoder said in Searching bar in tree widget:
But i want to see every item that contains the given text
-
@suslucoder Yes, I do - you want to select items, right? The method I suggested does exactly that...
-
@suslucoder said in Searching bar in tree widget:
I have a search bar, it doesnt set the contains this is my problem
Then can you please explain what you want to do with items you found? Because I don't understand...
-
QList<QTreeWidgetItem*> arama = ui->treeWidget2->findItems(ui->ara->text(), Qt::MatchContains|Qt::MatchRecursive, 0); for(QTreeWidgetItem* item : arama) { ui->treeWidget2->setCurrentItem(item, 0); // item->setBackground(0, Qt::cyan); }
when i comment item->setBackground(0, Qt::cyan);, unless the tree has more than 1 item that at give via ara->text, it show the exactly 1 item.
When i un comment the
item->setBackground(0, Qt::cyan);
the code finds the every item that contains the text, but the text setCurrent item has is blue, the other one is cyan.
When i only wrote
item->setBackground(0, Qt::cyan);
search doesnt work
-
@suslucoder Why do you call setCurrentItem?
-
@suslucoder said in Searching bar in tree widget:
But when i delete it, it doesnt work
In what way it doesn't work? setBackground should work without setCurrentItem, they are unrelated.
-
@suslucoder I debug and, it finds the items but doesnt show me on tree widget
-
Hi,
Why don't you just modify the background of each item of the list and nothing else ?
-
@suslucoder said in Searching bar in tree widget:
When i only wrote
item->setBackground(0, Qt::cyan);search doesnt work
I think arguement for setBackground is QBrush not the color
void QTreeWidgetItem::setBackground(int column, const QBrush &brush) -
@nagesh said in Searching bar in tree widget:
I think arguement for setBackground is QBrush not the color
Can you please elaborate this?
https://doc.qt.io/qt-5/qbrush.html#QBrush-3 -
@SGaist when i modify the background, for highligting the searching items,
even the text was changed, it keep the oldest item.I search for a and highligh its background.
Then i delete a, search for bIt shows me the both a and b
-
@Christian-Ehrlicher thanks for the correction.
@suslucoder it's showing old search items with different color because you have set it's background color in last search..
It's better to set all with default color and matched items in highlight colour for each search