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. Iterating through columns of selected row in QTableView
Forum Updated to NodeBB v4.3 + New Features

Iterating through columns of selected row in QTableView

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 2 Posters 840 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.
  • S Offline
    S Offline
    SPlatten
    wrote on 10 May 2021, 07:47 last edited by SPlatten 5 Oct 2021, 08:09
    #1

    I have a lambda slot connected to QTableView::doubleClicked:

    QObject::connect(mptvRecs, &QTableView::doubleClicked,
                     [this](const QModelIndex& crIndex) {
        const int intRow(crIndex.row());
        const int intColumns(this->mpsiModel->columnCount());
        QAbstractItemDelegate* pidRow(this->mptvRecs->itemDelegateForRow(intRow));
        for( int intCol=0; intCol<intColumns; intCol++ ) {
        ...
        }
    });
    

    What I want to do is iterate through each of the columns in the selected row. I'm not sure if I'm on the right track or not here...
    [Edit]

    QObject::connect(mptvRecs, &QTableView::doubleClicked,
                     [this](const QModelIndex& crIndex) {
        const int intColumns(this->mpsiModel->columnCount());
        QItemSelectionModel* pobjSel(this->mptvRecs
                                         ->sectionModel());
        if ( pobjSel == nullptr ) {
            return;
        } 
        for( int intCol=0; intCol<intColumns; intCol++ ) {
            QModelIndexList objColumn(pobjSel->selectedRows(intCol));
            ...
        }
    });
    

    So far its progressing objColumn looks correct as when I've single stepped, both r and c are correct.

    How would I get the column text / value from objColumn ?

    Kind Regards,
    Sy

    A 1 Reply Last reply 10 May 2021, 08:16
    0
    • S SPlatten
      10 May 2021, 07:47

      I have a lambda slot connected to QTableView::doubleClicked:

      QObject::connect(mptvRecs, &QTableView::doubleClicked,
                       [this](const QModelIndex& crIndex) {
          const int intRow(crIndex.row());
          const int intColumns(this->mpsiModel->columnCount());
          QAbstractItemDelegate* pidRow(this->mptvRecs->itemDelegateForRow(intRow));
          for( int intCol=0; intCol<intColumns; intCol++ ) {
          ...
          }
      });
      

      What I want to do is iterate through each of the columns in the selected row. I'm not sure if I'm on the right track or not here...
      [Edit]

      QObject::connect(mptvRecs, &QTableView::doubleClicked,
                       [this](const QModelIndex& crIndex) {
          const int intColumns(this->mpsiModel->columnCount());
          QItemSelectionModel* pobjSel(this->mptvRecs
                                           ->sectionModel());
          if ( pobjSel == nullptr ) {
              return;
          } 
          for( int intCol=0; intCol<intColumns; intCol++ ) {
              QModelIndexList objColumn(pobjSel->selectedRows(intCol));
              ...
          }
      });
      

      So far its progressing objColumn looks correct as when I've single stepped, both r and c are correct.

      How would I get the column text / value from objColumn ?

      A Offline
      A Offline
      artwaw
      wrote on 10 May 2021, 08:16 last edited by
      #2

      @SPlatten said in Iterating through columns of selected row in QTableView:

      QModelIndexList

      QModelIndexList::siblingAtColumn(int column)?

      For more information please re-read.

      Kind Regards,
      Artur

      S 1 Reply Last reply 10 May 2021, 08:32
      0
      • A artwaw
        10 May 2021, 08:16

        @SPlatten said in Iterating through columns of selected row in QTableView:

        QModelIndexList

        QModelIndexList::siblingAtColumn(int column)?

        S Offline
        S Offline
        SPlatten
        wrote on 10 May 2021, 08:32 last edited by
        #3

        @artwaw , thank you, where or how do I apply that, is my logic very wrong?

        Kind Regards,
        Sy

        A 1 Reply Last reply 10 May 2021, 09:17
        0
        • S SPlatten
          10 May 2021, 08:32

          @artwaw , thank you, where or how do I apply that, is my logic very wrong?

          A Offline
          A Offline
          artwaw
          wrote on 10 May 2021, 09:17 last edited by
          #4

          @SPlatten That depends. I personally never worked with QModelIndexList.
          My usual use case is with QSqlTabelModel in data() method when I need to consider hidden columns before returning data for visible column. Maybe our more experienced colleagues could offer more insight.

          Having said that - I don't think your logic is off here. Depending what you want to do while iterating here, you should be able to at least get the data.

          For more information please re-read.

          Kind Regards,
          Artur

          S 1 Reply Last reply 10 May 2021, 09:51
          0
          • A artwaw
            10 May 2021, 09:17

            @SPlatten That depends. I personally never worked with QModelIndexList.
            My usual use case is with QSqlTabelModel in data() method when I need to consider hidden columns before returning data for visible column. Maybe our more experienced colleagues could offer more insight.

            Having said that - I don't think your logic is off here. Depending what you want to do while iterating here, you should be able to at least get the data.

            S Offline
            S Offline
            SPlatten
            wrote on 10 May 2021, 09:51 last edited by
            #5

            @artwaw, thank you, done now:

            const int intColumns(this->mpsiModel->columnCount());
                QItemSelectionModel* pobjSel(this->mptvRecs
                                                 ->sectionModel());
                if ( pobjSel == nullptr ) {
                    return;
                } 
                for( int intCol=0; intCol<intColumns; intCol++ ) {
                    QModelIndexList objColumn(pobjSel->selectedRows(intCol));
                    for( int c=0; c<objColumn.count(); c++ ) {
                        QString strItem(objColumn.at(c).data().toString());
                        ...
                    }
                }
            

            Kind Regards,
            Sy

            1 Reply Last reply
            0

            1/5

            10 May 2021, 07:47

            • Login

            • Login or register to search.
            1 out of 5
            • First post
              1/5
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved