Important: Please read the Qt Code of Conduct -

Search between labels

  • hi
    i have some QLabels,
    i wanna create a field which when i type in it and press "enter" it goes through labels and prints the search results in a new frame. something like a very simple search engine.

    i really need that.

    any help would be appreciated.

  • Moderators

    So what's the question? :)

  • To implement this task QRegExp class is your friend.
    put all the labels in a QList and loop through this list, this will match the text in the search field with each label's text. If the text matches, append the matched label to another QList. Finally put all the matched label to a separate QFrame.
    here is "for loop"
    QRegExp rx("");
    // call this function when user presses enter key and
    //pass the text in the search field
    void searchLabels(QString name) {
    //list contains all the labels
    for (int i = 0; i < list.length(); i++) {
    //searchField is object of QLineEdit
    if (rx.indexIn(list[i]->text()) != -1) {
    // appned the label at list[i] to new QList
    // or you can append new label to the
    // list (depends upon your needs)

    I am not a C++ developer. if you find syntax problems, sorry for that.

Log in to reply