Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QTableview Search



  • Hello everyone, i am doing a search engine for a QTableview, and this is what i have until now:

    void Inventory::on_Search_clicked(){
    
        QModelIndexList matchList = ui->tableView->model()->match(ui->tableView->model()->index(0,0), Qt::EditRole, ui->lineEdit->text(), -1,  Qt::MatchFlags(Qt::MatchContains|Qt::MatchWrap));
    
        if(matchList.count()>=1){
         
                ui->tableView->setCurrentIndex(matchList.first());
                ui->tableView->scrollTo(matchList.first()); 
                }
        
        QSound::play("/home/adan/Groostore/Resources/Sounds/Update.wav");
    }
    

    It works as required, but i want to add another functionality:

    This code search for specific matches with a QLineedit and it selects and scrolls to the first value of the QModelindexList, but, if i click a second time searching the same text on the QLineedit, i would like the selection to scroll to the 2 item on the QModelindexList, i have tried many things with no luck, i hope you can help on this, Thanks!



  • Well, at the end i figured it out:

    void Registry::on_Search_clicked(){
    
        from++;
        QString text = ui->lineEdit_2->text();
        if(text.isEmpty())
                    return;
                QModelIndexList indexes = ui->tableView->model()->match(ui->tableView->model()->index(0,0),Qt::EditRole,text,-1, Qt::MatchFlags(Qt::MatchContains|Qt::MatchWrap));
    
                if(indexes.length() > from){
                    QModelIndex ix = indexes.at(from);
                    ui->tableView->setCurrentIndex(ix);
                    ui->tableView->scrollTo(ix);
                }
    
                QSound::play("/home/adan/Groostore/Resources/Sounds/Update.wav");
    }
    

    The int variable "from" is declared as public on the header.

    Thanks!


  • Lifetime Qt Champion

    Hi,

    Keep the current search as well as the index of the item in it that you are showing. Then have a next button that will increment that index and set the focus on the new "current" index.



  • @SGaist i was thinkng about it, but, is there a way to do it using the just the Search button? jeje


  • Lifetime Qt Champion

    Yes, do the same and add the logic to reset the state once you change the text searched.



  • @SGaist I cannot do the logic of it, its driving me crazy xd


  • Lifetime Qt Champion

    What part are you failing on ?



  • Well, at the end i figured it out:

    void Registry::on_Search_clicked(){
    
        from++;
        QString text = ui->lineEdit_2->text();
        if(text.isEmpty())
                    return;
                QModelIndexList indexes = ui->tableView->model()->match(ui->tableView->model()->index(0,0),Qt::EditRole,text,-1, Qt::MatchFlags(Qt::MatchContains|Qt::MatchWrap));
    
                if(indexes.length() > from){
                    QModelIndex ix = indexes.at(from);
                    ui->tableView->setCurrentIndex(ix);
                    ui->tableView->scrollTo(ix);
                }
    
                QSound::play("/home/adan/Groostore/Resources/Sounds/Update.wav");
    }
    

    The int variable "from" is declared as public on the header.

    Thanks!


  • Lifetime Qt Champion

    There's no need to declare it as public, it's an internal implementation detail that should not be exposed.

    Note that you don't seem to reset from anywhere so you will likely have strange results. Like for example always starting to show the second found entry.

    Another issue here is that you re-do the search every time so if you have a big table you are killing performances. You might want to take some time to re-think the search functionality.



  • @SGaist Hello, the from variable is being reseted on a QlineEdit slot called textChanged; where can i declare the from variable to avoid making it public? because everytime the Search button event is closed the variable int is reseted, and i need to keep it until textChanged is called. Thanks


  • Lifetime Qt Champion

    @Adan895
    Hi
    The from++; variable should be a member of the class. Just placed in the
    private section and not public since its used for something internal and
    the outside world should not have access to it. Only the class.


Log in to reply