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 programmatically select new row in TableView when row is added to TableModel
QtWS25 Last Chance

how to programmatically select new row in TableView when row is added to TableModel

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 8.5k Views
  • 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.
  • C Offline
    C Offline
    Chris Bare
    wrote on last edited by
    #1

    I have a tableView subclass which I set up like this:

    auto *model = new QSortFilterProxyModel(parent);
    model->setSourceModel(myModel);
    model->setFilterKeyColumn(0);
    myTableView->setModel(model);
    

    selectionMode = SingleSelection
    selectionBehavior = SelectRows
    sortingEnabled = true

    My model is a subclass of QAbstractTableModel. I use this function (triggered by a menu item) to add data:

    void ActionModel::addAction (Action *act)
    {
        int newRow = actions.count();
        beginInsertRows(QModelIndex(), newRow, newRow);
        actions.append(act);
        endInsertRows();
    }
    

    This works perfectly. The new data shows up in my Table View and I can sort it by each column.
    I want to programmatically select the row for the new entry. I assume I should call:

    void QTableView::selectRow(int row);
    

    but because the View might be sorted, I don't know which row number to select.
    How do I find the row in the view that corresponds to the row in my model?

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi
      You mean to know its original index unsorted ?
      Maybe this does the trick ?
      http://doc.qt.io/qt-5/qsortfilterproxymodel.html#mapToSource

      C 1 Reply Last reply
      1
      • mrjjM mrjj

        Hi
        You mean to know its original index unsorted ?
        Maybe this does the trick ?
        http://doc.qt.io/qt-5/qsortfilterproxymodel.html#mapToSource

        C Offline
        C Offline
        Chris Bare
        wrote on last edited by
        #3

        @mrjj Not exactly, I want to know the row number where the new data item is displayed. Actually I don't care where it is, I just want it selected, but the only interface I've found is QTableView::selectRow(int row)
        It looks like mapSelectionFromSource might work, but I don't know how to get a QItemSelection for the data in my model.

        1 Reply Last reply
        0
        • C Offline
          C Offline
          Chris Bare
          wrote on last edited by
          #4

          I found a solution that mostly works. When I add the new data to my model, I return a QModelIndex that I later pass to the QSortFilterProxyModel->mapFromSource () which gives me the QModelIndex of the matching item in the view. Then you have to use the tableView's selectionModel to select it.

          QModelIndex ActionModel::addAction (Action *act)
          {
              int newRow = actions.count();
              beginInsertRows(QModelIndex(), newRow, newRow);
              actions.append(act);
              endInsertRows();
          	return (index(newRow,0));
          }
          
          tableView->selectionModel()->select (proxy->mapFromSource (qmi), QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
          

          This all works exactly as I want if I don't sort the table.
          If I add 2 rows, then sort the table and add a 3rd row, the second row added shows a light blue on only the first column.

          0_1526177896170_tableview.png

          See how the Baker field is rendered? It goes away when the window does not have focus. Any idea what it is or how to get rid of it?

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

            The light blue cell is the cell which has the current index.

            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
            3
            • C Offline
              C Offline
              Chris Bare
              wrote on last edited by
              #6

              Thanks, I changed:

              ui->actionListView->selectionModel()->select (proxy->mapFromSource (qmi), QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
              

              to

              ui->actionListView->setCurrentIndex (proxy->mapFromSource (qmi));
              

              And now it is working as I expected. I didn't realize that the current index and the selection are 2 different things.

              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