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 843 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.
  • SPlattenS Offline
    SPlattenS Offline
    SPlatten
    wrote on last edited by SPlatten
    #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

    artwawA 1 Reply Last reply
    0
    • SPlattenS SPlatten

      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 ?

      artwawA Offline
      artwawA Offline
      artwaw
      wrote on 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

      SPlattenS 1 Reply Last reply
      0
      • artwawA artwaw

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

        QModelIndexList

        QModelIndexList::siblingAtColumn(int column)?

        SPlattenS Offline
        SPlattenS Offline
        SPlatten
        wrote on last edited by
        #3

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

        Kind Regards,
        Sy

        artwawA 1 Reply Last reply
        0
        • SPlattenS SPlatten

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

          artwawA Offline
          artwawA Offline
          artwaw
          wrote on 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

          SPlattenS 1 Reply Last reply
          0
          • artwawA artwaw

            @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.

            SPlattenS Offline
            SPlattenS Offline
            SPlatten
            wrote on 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

            • Login

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