Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] Searching a string in a QtableView or QStandardItemModel
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] Searching a string in a QtableView or QStandardItemModel

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 9.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    amban
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • G Offline
      G Offline
      giesbert
      wrote on last edited by
      #2

      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?

      Nokia Certified Qt Specialist.
      Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

      1 Reply Last reply
      0
      • A Offline
        A Offline
        amban
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • A Offline
          A Offline
          amban
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          • C Offline
            C Offline
            confused
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved