Results 1 to 6 of 6

Thread: Searching Button Text

  1. #1

    Question Searching Button Text

    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.

    capture1.jpg

    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

  2. #2
    Join Date
    Dec 2016
    Posts
    16
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4

    Default Re: Searching Button Text

    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.

  3. #3

    Default Re: Searching Button Text

    @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

  4. #4

    Default Re: Searching Button Text

    Okay, I was able to get help and got this worked out and working by:

    void MainWindow:n_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!

  5. #5

    Default Re: Searching Button Text

    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::A lignTop);

    It keeps the searched exe's in the middle

  6. #6

    Default Re: Searching Button Text

    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.

Similar Threads

  1. push button twice different text
    By Kloster in forum Newbie
    Replies: 10
    Last Post: 2nd September 2016, 18:17
  2. Replies: 2
    Last Post: 21st January 2016, 17:16
  3. Changing text of button in no relation to button
    By Sabre Runner in forum Newbie
    Replies: 22
    Last Post: 23rd September 2010, 12:29
  4. Replies: 6
    Last Post: 21st August 2010, 21:09
  5. Button Text Position
    By Slewman in forum Qt Tools
    Replies: 1
    Last Post: 29th September 2009, 16:35

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.