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. QTableView addressing data
Forum Updated to NodeBB v4.3 + New Features

QTableView addressing data

Scheduled Pinned Locked Moved Solved General and Desktop
13 Posts 4 Posters 929 Views 3 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.
  • Christian EhrlicherC Offline
    Christian EhrlicherC Offline
    Christian Ehrlicher
    Lifetime Qt Champion
    wrote on last edited by
    #2

    https://doc.qt.io/qt-5/qstandarditemmodel.html#data

    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
    • SPlattenS SPlatten

      I have a table view and model:

      QStandardItemModel* mpsiSDmodel;
      QTableView* mptvSDrecs;
      

      I create the table view and model and set model.

      mptvSDrecs = new QTableView(this);
      mptvSDrecs->setSelectionBehaviour(QAbstractItemView::SelectRows);
      mptvSDrecs->setStyleSheet(DataSets::mscszTableViewStyle);
      mpsiSDmodel = new QStandardItemModel(this);
      

      I've hidden the vertical headers and set style sheet for horizontal headers. I've connected a slot to the table view to manage double clicks. I've populated the table with rows and columns.

      What I am struggling with is how to get access to the data in the slot when a row is double clicked, my lambda slot:

      [this](const QModelIndex& crIndex) {
          QItemSelectionModel* pobjSModel(mptvSDrecs->selectionModel());
          QModelIndexList objSRows(pobjSModel->selectedRows());
          if ( objSRows.count() == 0 ) {
              return;
          }
      }
      

      I'm finding it difficult to establish how I address a cell in the table or a specific column header. I've been looking around for examples of which there is lots of code but nothing specific.

      mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #3

      @SPlatten
      Hi
      Well when you are using Views, then data is in the model so you talk to the model to get it.

      https://doc.qt.io/qt-5/qstandarditemmodel.html#data

      So you just use the ModelIndex with the data function to get or set data as you wish.

      hehe ninja'd. Mr Ehrlicher was faster :)

      1 Reply Last reply
      2
      • SPlattenS Offline
        SPlattenS Offline
        SPlatten
        wrote on last edited by
        #4

        @Christian-Ehrlicher said in QTableView addressing data:

        https://doc.qt.io/qt-5/qstandarditemmodel.html#data

        I've added to my slot:

        const int cintColumns(mpsiSDmodel->columnCount()); // 5 columns
        for( int intCol=0; intCol<cintColumns; intCol++ ) {
            QVariant varData(mpsiSDmodel->data(objSRows.at(intCol)));
            qDebug << varData;
        }
        

        The first iteration of the above works and displays he correct data, the second call results in a crash, with:

        ASSERT failure in QList<T>::at "index out of range", file [path]/qlist.h line 541
        

        All of the data and the headers are displayed correctly.

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

          And why do you tell us this instead checking the size of objSRows by yourself?

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          SPlattenS 1 Reply Last reply
          0
          • Christian EhrlicherC Christian Ehrlicher

            And why do you tell us this instead checking the size of objSRows by yourself?

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

            @Christian-Ehrlicher , because I've spent hours doing just that and not getting anywhere as I said the data is displayed correctly I'm not getting anywhere.

            The debugger isn't very useful, if I expand the QList<QModelIndex> it contains one item, thats as far as I get, I don't know why or how to fix it so it contains more...thats why I'm asking for assistance.

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

              I really don't understand your problem. QList tells you that you give them an index which is out of it's size. So the only place where you access such a list is here

              objSRows.at(intCol)

              So the only reason can be that intCol is greater or equal the size of objSRows... basic c++ stuff.

              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
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #8

                Hi,

                @SPlatten said in QTableView addressing data:

                objSRows

                As the documentation of selectedRows says:

                Returns the indexes in the given column for the rows where all columns are selected.
                

                By default it's the column zero, from the looks of it you are trying to use it as if your where given the complete rows which is not the case.

                As already pointed in one of your other threads: use QModelIndex::siblingAtRow.

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                SPlattenS 3 Replies Last reply
                2
                • SPlattenS Offline
                  SPlattenS Offline
                  SPlatten
                  wrote on last edited by
                  #9
                  This post is deleted!
                  1 Reply Last reply
                  0
                  • SGaistS SGaist

                    Hi,

                    @SPlatten said in QTableView addressing data:

                    objSRows

                    As the documentation of selectedRows says:

                    Returns the indexes in the given column for the rows where all columns are selected.
                    

                    By default it's the column zero, from the looks of it you are trying to use it as if your where given the complete rows which is not the case.

                    As already pointed in one of your other threads: use QModelIndex::siblingAtRow.

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

                    @SGaist , in my code I've specified that a whole row is selected and visually when I click on the row the whole row is selected, the row contains 5 columns.

                    In the slot when I double click as my slot code shows I call the selectedRows function which returns an instance of QModelIndexList, however this only contains 1 item so is that the selected row ?

                    How to I expand on that 1 item to get the list of columns?

                    1 Reply Last reply
                    0
                    • SGaistS SGaist

                      Hi,

                      @SPlatten said in QTableView addressing data:

                      objSRows

                      As the documentation of selectedRows says:

                      Returns the indexes in the given column for the rows where all columns are selected.
                      

                      By default it's the column zero, from the looks of it you are trying to use it as if your where given the complete rows which is not the case.

                      As already pointed in one of your other threads: use QModelIndex::siblingAtRow.

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

                      @SGaist , if a table has a horizontal header, is the first data row actually 1?

                      As when I double click the first highlighted row the data returned is 1.

                      1 Reply Last reply
                      0
                      • SGaistS SGaist

                        Hi,

                        @SPlatten said in QTableView addressing data:

                        objSRows

                        As the documentation of selectedRows says:

                        Returns the indexes in the given column for the rows where all columns are selected.
                        

                        By default it's the column zero, from the looks of it you are trying to use it as if your where given the complete rows which is not the case.

                        As already pointed in one of your other threads: use QModelIndex::siblingAtRow.

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

                        @SGaist , Yes, I can see whats happening now and I've checked the documentation it doesn't say anything that I could see.

                        When I call:

                        QModelIndexList objIndexes = pobjSDModel->selectedIndexes();
                        

                        The actual index returned is base 1, the first row of data after the headers has a row index of 1, although you cannot use 1 to address the row in the QModelIndex::index function.

                        It may be that I've done something wrong that would cause this behaviour.

                        Have rewritten slot now to:

                        QItemSelectionModel* pobjModel(mptvSDrecs->selectionModel());
                        QModelIndexList objIndexes(pobjModel->selectedIndexes());
                        for( int intCol=0; intCol<obIndexes.length(); intCol++ ) {
                            QModelIndex objIndex(objIndexes[intCol]),
                                        objCell = mpsiSDmodel->index(objIndex.row(), intCol);
                            qDebug() << objCell;
                        }
                        

                        Now it works!

                        1 Reply Last reply
                        1
                        • SGaistS Offline
                          SGaistS Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on last edited by SGaist
                          #13

                          The method returns the indexes of the column requested for the rows that are completely selected. It's a "vertical" list since it's for a column.

                          Interested in AI ? www.idiap.ch
                          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                          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