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. How to iterate through items in a qtableView to determine if a number exists
Forum Updated to NodeBB v4.3 + New Features

How to iterate through items in a qtableView to determine if a number exists

Scheduled Pinned Locked Moved General and Desktop
6 Posts 2 Posters 16.6k 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.
  • F Offline
    F Offline
    fs_tigre
    wrote on last edited by
    #1

    Hi,

    How can I iterate through items in a qtableView to determine if a number in a row matches an external number. In other words I would like to go through the tableView and see if one of the rows contain a certain number.

    How can I do what I'm describing above?

    FYI,

    I'm using QStandardItemModel.

    Thanks a lot.

    1 Reply Last reply
    2
    • F Offline
      F Offline
      fs_tigre
      wrote on last edited by
      #2

      Here is what I came up with that works for what I want to do.

      @void MainWindow::findNumber()
      {
      int row = model->rowCount();
      int col = model->columnCount();
      for (int i = 0; i < row ; ++i)
      {
      for (int j = 0; j < col; j++)
      {
      QVariant content = model->data(model->index(i, j), Qt::DisplayRole);
      if(content == QVariant("5"))
      {
      qDebug()<<"Found a number 5";
      }else
      {
      qDebug()<<"There are no number 5s in this table";
      }

          }
      }
      

      }@

      1 Reply Last reply
      2
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

        You can iterate through all indexes from the row and check each entry for the value you are searching. Maybe not the most efficient but it does the job.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • F Offline
          F Offline
          fs_tigre
          wrote on last edited by
          #4

          What is the most efficient way, the way I did it or the way you are suggesting?

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Sorry, i didn't saw your update after sending my answer which is the same as yours but you where only talking about a row not searching the entire model.

            You could try using QtConcurrent to speed things up but that would complicate your code a bit

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0
            • F Offline
              F Offline
              fs_tigre
              wrote on last edited by
              #6

              Thanks a lot for your help!

              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