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

[SOLVED] Searching a string in a QtableView or QStandardItemModel



  • Hi All,
    I want to search for a string in my Qtableview - What is the best way to way that ? Currently i do the simple C++ string compare way which is probably an incorrect answer.

    Thanks



  • Hi,

    you can ask for the value of the cell a, convert it to QString and compare it.
    What else do you want to do?



  • Hi,
    Actually i wanted to do it using QSortFilterProxyModel. Kindly rectify what is wrong :

    @QString input = findstring->text().trimmed();
    QSortFilterProxyModel proxy;
    proxy.setSourceModel(mytablemodel);
    proxy.setFilterFixedString(input);

     int num_rows = mytablemodel->rowCount();
     int num_cols = mytablemodel->columnCount();
    
     QModelIndex matchingIndex = proxy.mapFromSource(proxy.index(num_rows,num_cols));
     if(matchingIndex.isValid())
     {
         QMessageBox::information(this, "Find", "Found");
     }
     else
     {
         QMessageBox::information(this, "Find", "Not Found");
     }
    

    @

    I am setting the index incorrectly i guess. How to browse the entire table ?

    I truely appreciate your help.



  • Hi All,
    It was a minor mistake on my side instead of :
    @QModelIndex matchingIndex = proxy.mapFromSource(proxy.index(num_rows,num_cols))@

    it must be....
    @QModelIndex matchingIndex = proxy.mapToSource(proxy.index(num_rows,0))@

    Thanks



  • Hey amban did you have more code to get this to work? Becase I am trying to do the same thing but anything I type in, if its in the table or not, always comes up as Not Found.


Log in to reply