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. Select row and column from a table at the same time [SOLVED]
Qt 6.11 is out! See what's new in the release blog

Select row and column from a table at the same time [SOLVED]

Scheduled Pinned Locked Moved General and Desktop
5 Posts 4 Posters 3.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.
  • S Offline
    S Offline
    salvador
    wrote on last edited by
    #1

    Hi,
    i am generating a grid. The grid is a qTableView item. I want when the window open the first row and column to be highlighted. I write these two lines for this purpose:
    @ui->tableView->selectRow(0);
    ui->tableView->selectColumn(0);@

    but only the first column appears highlighted. As far as i understand when we do a selection it erases the previous selection. Is there a way to make it remember the selection of the row so i have the desired outcome?

    Thanks!

    1 Reply Last reply
    0
    • B Offline
      B Offline
      b1gsnak3
      wrote on last edited by
      #2

      You must use "setSelectionModel":http://qt-project.org/doc/qt-4.8/qabstractitemview.html#setSelectionModel with a custom "QItemSelectionModel":http://qt-project.org/doc/qt-4.8/qitemselectionmodel.html

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

        QItemSelectionModel enables you to define the current selection very flexibly on a per-item basis:

        @
        QItemSelectionModel* selection = myView->selectionModel();

        // Get model indexes to select

        // For all indexes
        selection->select(index, QItemSelectionModel::Select);
        @

        The example above is rather simple. Using QItemSelection and QItemSelectionRange with the selection model you can solve your problem a lot more elegantly.

        1 Reply Last reply
        0
        • S Offline
          S Offline
          salvador
          wrote on last edited by
          #4

          nice, thanks a lot

          1 Reply Last reply
          0
          • podsvirovP Offline
            podsvirovP Offline
            podsvirov
            wrote on last edited by
            #5

            Code example:

            @
            #include <QApplication>
            #include <QTableWidget>

            int main(int argc, char *argv[])
            {
            int rows = 5, columns = 3;

            QApplication a(argc, argv);
            
            QTableWidget table(rows, columns);
            
            for(int i = 0; i < rows; i++) {
                for(int j = 0; j < columns; j++)
                    table.setItem(i, j, new QTableWidgetItem(
                                      QString("Item (%1, %2)")
                                      .arg(i + 1).arg(j + 1)));
            }
            
            QModelIndex zero = table.model()->index(0, 0);
            QModelIndex right = table.model()->index(0, columns - 1);
            QModelIndex bottom = table.model()->index(rows - 1, 0);
            
            QItemSelection selection(zero, right);
            selection.merge(QItemSelection(zero, bottom), QItemSelectionModel::Select);
            table.selectionModel()->select(selection, QItemSelectionModel::Select);
            
            table.resizeRowsToContents();
            table.resizeColumnsToContents();
            table.show();
            
            return a.exec&#40;&#41;;
            

            }
            @

            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