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 select several cells from a QTableWidget?
Forum Updated to NodeBB v4.3 + New Features

How to select several cells from a QTableWidget?

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 4 Posters 3.9k Views 2 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.
  • X Offline
    X Offline
    xtingray
    wrote on last edited by xtingray
    #1

    Hi,

    I need to select an specific range of cell items in a QTableWidget object directly from my code. My input is a list with the cell coordinates (int column. int row) that makes part of the selection. Example of input: [0, 1] [0, 2] [0, 3]

    Any hint?

    PS: I found this piece of code and I wonder if there's a way to create my own QItemSelection object, assigning the items I want to select. Is it possible?

    QItemSelection selectedItems = tableView->selectionModel()->selection();
    tableView->selectionModel()->clearSelection();
    tableView->selectionModel()->select(selectedItems, QItemSelectionModel::Select);
    
    

    Qt Developer

    1 Reply Last reply
    0
    • michalosM Offline
      michalosM Offline
      michalos
      wrote on last edited by
      #2

      Have You read the QItemSelectionModel reference page?
      It seems to be what You need.
      http://doc.qt.io/qt-5/qitemselectionmodel.html

      1 Reply Last reply
      3
      • VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by
        #3
        void selectIndexes(const QModelIndexList& indexes){
        tableWidget->selectionModel()->clearSelection();
        for(auto& singleIdx : indexes)
        tableWidget->selectionModel(singleIdx , QItemSelectionModel::Select);
        }
        

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        1 Reply Last reply
        1
        • X Offline
          X Offline
          xtingray
          wrote on last edited by
          #4

          I know I 'm in the right direction, but there is a point where I got stuck: if I can instance items of the QModelIndex class using the coords I already have, I can solve my problem easily as in the example you suggest... but how?

          Checking the QModelIndex method s, there is no set actions of any kind. How can I define a QModelIndex with the cell position (column, row), i.e. (0,1)? I have been reading the view/model documentation but I must confess I am lost about this specific topic :(


          Qt Developer

          VRoninV 1 Reply Last reply
          0
          • X xtingray

            I know I 'm in the right direction, but there is a point where I got stuck: if I can instance items of the QModelIndex class using the coords I already have, I can solve my problem easily as in the example you suggest... but how?

            Checking the QModelIndex method s, there is no set actions of any kind. How can I define a QModelIndex with the cell position (column, row), i.e. (0,1)? I have been reading the view/model documentation but I must confess I am lost about this specific topic :(

            VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by
            #5

            @xtingray said in How to select several cells from a QTableWidget?:

            I can instance items of the QModelIndex class using the coords I already have, I can solve my problem easily as in the example you suggest... but how?

            easy!
            tableWidget->model()->index(row,column);

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            1 Reply Last reply
            1
            • E Offline
              E Offline
              Elbujito
              wrote on last edited by
              #6

              Don't forget to set :

              void setSelectionMode(QAbstractItemView::SelectionMode mode)
              setSelectionBehavior(QAbstractItemView::SelectRows);

              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