PDA

View Full Version : Searching Button Text



Epiales666
3rd October 2017, 22:24
Hey all...

Very new to QT. I always wanted to learn to program and stuff, so here I am.

I created a simple UI that has a list of buttons that will open my programs on my computer. Pretty simple.

12602

I just need a way to use a search feature in order to find the program I'm looking for as I type it in the search bar.

Any ideas? Thx all!

Epiales666

ednakram
3rd October 2017, 23:02
I would suggest a "filter" feature rather than a "search" feature for this kind of functionality. Namely once the user types into the search bar only the matching application buttons are shown and others are hidden.

Here's what you need to do:

1. Make sure that the "search" is not fired on every keystroke immediately, but rather when the last keystroke happened let us say 50ms ago, fire the search. (You can use a QTimer to achieve this - create QTimer with 50ms timeout interval, keep starting the QTimer on every keystroke. On the timeout signal of the QTimer, construct a QRegExp with the pattern that the user typed and emit a signal "startSearch(const QRegExp& pattern)". Connect this signal to the "searchApps" slots (mentioned in #3 below).

2. While adding these buttons, you update a QList<QPair<QString, QWidget*> > when buttons are created, where QString is the name of the app and QWidget* is the pointer to the actual QButton widget.

3. Now for the "searchApps" slot. This function should go over the above QList elements and try to match the "QRegExp pattern" with each of the QString's i.e. app names (first item in QPair). For the ones that match call `QWidget::setVisible(true)` using the pointer (second in QPair) and ones that dont match it call `QWidget::setVisible(false)`.

That is it. All the best.

To make it a "search" feature as you suggest instead - you will need a different approach - (you could still use some of the ideas I suggested above about the QList and the QTimer):
1. Search generally works by highlighting matched text and providing back/forward buttons.
2. You need back/forward buttons somewhere to move the search forward/backwards.
3. You would need to highlight all the "matched" applications button names - by changing their background to yellow or some other visible color.
4. You would move through the matches with the next/previous buttons - by "scrolling" to the next/previous matched item respectively.

Epiales666
3rd October 2017, 23:50
@02ednakram

haha... I'm so grateful you replied and took the time to think about every feature. It's rare that people take the time to actually be so considerate: Thank you! Problem is, your answer wasn't newbie answered lol. I'm basically, let's say, day one using this with no programming abilities basically. I'm extremely computer savy though. I've used QT for simple projects like creating my own personal movie viewer for my backed up movies, but the developer does most of the work for me lol.

Again, thank you for your kindness.. I'll be googling and trying to see if I can't find out how to work this. Thanks m8!

Epiales666

Epiales666
4th October 2017, 04:12
Okay, I was able to get help and got this worked out and working by:


void MainWindow::on_lineEdit_textChanged(const QString &search)
{

QList<QLabel *> labels = findChildren<QLabel *>();
QList<QPushButton *> buttons = findChildren<QPushButton *>();

// handle empty search by showing all buttons and exiting
if (search.isEmpty())
{
foreach (QPushButton *b, buttons)
b->show();
foreach (QLabel *b, labels)
b->show();

return;
}

// search buttons for any matching "search" and hide everything not matching
foreach (QPushButton *b, buttons)

{
if (b->text().contains(search, Qt::CaseInsensitive))
b->show();
else
b->hide();



foreach (QLabel *b, labels)

{
if (b->text().contains(search, Qt::CaseInsensitive))
b->show();
else
b->hide();
}
}
}

I do however have on issue left, if anyone has any ideas. When I search, it searches fine and removes the other buttons and icons, but the search results are not displayed at the top, but in the middle. Any idea how to fix this? I thought it might just be an alignment problem in the scroll area, but I set that to top and left and it didn't change anything. Any ideas? Thanks all!

Epiales666
4th October 2017, 11:42
Is it even possible to do that? Below is the code I"ve used and it should work, but doesn't.

ui->scrollArea->setAlignment(Qt::AlignLeading|Qt::AlignLeft|Qt::Al ignTop);

It keeps the searched exe's in the middle

Epiales666
5th October 2017, 21:20
Okay, I finally have it working. I actually used the developer to make and had snapped the items to the mainwindow grid. Of course that won't allow me to adjust, as it's in the mainwindows characteristics. Anyway, it's all good. I rewrote everything and it works great. Thanks for the suggestions and pointing in a direction to start.