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. QTableView: select row with a specific id
Forum Updated to NodeBB v4.3 + New Features

QTableView: select row with a specific id

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

    After sorting (i.e. ui->table->sortByColumn(MODEL_PACKAGES_COL_NAME)) the order of the rows in the QTableView may be different than those in the model. If I need to select (highlight) a row with a specific id, I do this:

    void DialogPackages::selectId(int id)
    {
        for (int i = 0; i < _model->rowCount(); i++)
        {
            QModelIndex index = _model->index(i, MODEL_PACKAGES_COL_ID);
            if (id == _model->data(index)) ui->table->selectRow(i);
        }
    }
    

    it works, but I wonder if there's something better to avoid to cycle all the rows every time.

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      I don't see another way, apart from that you should break out once the row is found.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      M 1 Reply Last reply
      1
      • Christian EhrlicherC Christian Ehrlicher

        I don't see another way, apart from that you should break out once the row is found.

        M Offline
        M Offline
        Mark81
        wrote on last edited by
        #3

        @Christian-Ehrlicher said in QTableView: select row with a specific id:

        I don't see another way, apart from that you should break out once the row is found.

        Yep, of course I forgot to break the loop.
        In other words, there are no efficient find methods built-in.

        1 Reply Last reply
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Mark81 said in QTableView: select row with a specific id:

          there are no efficient find methods built-in.

          build-in - no since it's your data, not something Qt has control of it.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          M 1 Reply Last reply
          0
          • Christian EhrlicherC Christian Ehrlicher

            @Mark81 said in QTableView: select row with a specific id:

            there are no efficient find methods built-in.

            build-in - no since it's your data, not something Qt has control of it.

            M Offline
            M Offline
            Mark81
            wrote on last edited by
            #5

            @Christian-Ehrlicher said in QTableView: select row with a specific id:

            build-in - no since it's your data, not something Qt has control of it.

            Not actually, I thought about a generic method like this:

            int QTableView::findRow(int col, QVariant data);
            

            it would return the (first) row number where data is present into column col. -1 otherwise.
            Or a more generic one:

            QList<int> QTableView::findRows(int col, QVariant data);
            

            of course it's trivial to implement them in a rough way like I did above.
            I was just afraid I didn't search well the documentation.

            1 Reply Last reply
            0
            • Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Maybe QAbstractItemModel::match() - but custom stuff is faster I would guess

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              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