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. Retrieving data from QModelIndex
QtWS25 Last Chance

Retrieving data from QModelIndex

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 2 Posters 174 Views
  • 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.
  • G Offline
    G Offline
    Gandalf404
    wrote on last edited by
    #1

    Hello everyone. I have QSqlRelationTableMode, which QModelIndex i pass to another widget.
    Is there a way (maybe simplier) to get data from QModelIndex except this:

    index.model()->index(index.row(), 0).data();
    

    and this:

    index.model()->data(index, Qt::DisplayRole);
    

    And is there a difference between these two approaches ?
    Thank you for answers !

    JonBJ 1 Reply Last reply
    0
    • G Gandalf404

      Hello everyone. I have QSqlRelationTableMode, which QModelIndex i pass to another widget.
      Is there a way (maybe simplier) to get data from QModelIndex except this:

      index.model()->index(index.row(), 0).data();
      

      and this:

      index.model()->data(index, Qt::DisplayRole);
      

      And is there a difference between these two approaches ?
      Thank you for answers !

      JonBJ Online
      JonBJ Online
      JonB
      wrote on last edited by
      #2

      @Gandalf404
      Yours are equivalent, but the first one arbitrarily picks column 0 when it could more generically pick index.column() for that.
      Your first way is overlong, second way is shorter.
      But QModelIndex lets you access the data() there directly via index.data() (defaults to Qt::DisplayRole). However you will need the second way if you want to call setData() rather than data().

      G 1 Reply Last reply
      1
      • JonBJ JonB

        @Gandalf404
        Yours are equivalent, but the first one arbitrarily picks column 0 when it could more generically pick index.column() for that.
        Your first way is overlong, second way is shorter.
        But QModelIndex lets you access the data() there directly via index.data() (defaults to Qt::DisplayRole). However you will need the second way if you want to call setData() rather than data().

        G Offline
        G Offline
        Gandalf404
        wrote on last edited by
        #3

        @JonB In my app i need to set a class members with data from specified columns. In my case it's better to use 1 approach ?

        JonBJ 1 Reply Last reply
        0
        • G Gandalf404

          @JonB In my app i need to set a class members with data from specified columns. In my case it's better to use 1 approach ?

          JonBJ Online
          JonBJ Online
          JonB
          wrote on last edited by JonB
          #4

          @Gandalf404
          No. Like I said whatever = index.data() since you already have a QModelIndex, what is not clear?
          Or whatever = model->data(somRow, someRolumn) if you don't happen to start with a QModelIndex.

          G 1 Reply Last reply
          0
          • JonBJ JonB

            @Gandalf404
            No. Like I said whatever = index.data() since you already have a QModelIndex, what is not clear?
            Or whatever = model->data(somRow, someRolumn) if you don't happen to start with a QModelIndex.

            G Offline
            G Offline
            Gandalf404
            wrote on last edited by
            #5

            @JonB Maybe you misunderstood. There is a class, that need to be set up:
            19c21141-0b56-4af5-979d-dcac621e61ff-image.png
            Set up with data from chosen row, (part id, part count etc.):
            cad4ef0b-b88f-47c4-b294-49d5b5b9ab44-image.png
            At second widget i only have a QModelIndex, so "model->data()' isn't working here. So i need need to get data from selected row and specified column
            So why 1 approach isn't good ?

            JonBJ 1 Reply Last reply
            0
            • G Gandalf404

              @JonB Maybe you misunderstood. There is a class, that need to be set up:
              19c21141-0b56-4af5-979d-dcac621e61ff-image.png
              Set up with data from chosen row, (part id, part count etc.):
              cad4ef0b-b88f-47c4-b294-49d5b5b9ab44-image.png
              At second widget i only have a QModelIndex, so "model->data()' isn't working here. So i need need to get data from selected row and specified column
              So why 1 approach isn't good ?

              JonBJ Online
              JonBJ Online
              JonB
              wrote on last edited by JonB
              #6

              @Gandalf404
              I will try once again. There are two calls you can use to access data at a row/column:

              • If you already have a QModelIndex for the desired row/column the shortest code you can use to read the data is index.data().
              • If you do not have a QModelIndex but have the desired row/column in some variables you can use model.data(row, column).

              If you have a desired QModelIndex index the following three expressions all return the same value:

              1. index.model()->index(index.row(), index.column()).data()
              2. index.model()->data(index)
              3. index.data()

              Which of these would you like to type into your code? There is nothing wrong with 1, but it is longer, and a tiny, tiny bit less efficient, than the totally equivalent 2. And similarly for 2 compared to 3. So I would choose 3.

              If you do not have a QModelIndex already to hand, but you know the row and column numbers you want, you could either create a QModelIndex index(row, column) in one statement and then use one the above in the following statement or you could use approach 1 something like:

              partId = model->data(someRow, 0);
              kitId = model->data(someRow, 1);
              partName = model->data(someRow, 2);
              ...
              

              If --- and I don't know whether this is you case, but it might be --- you write a function which is passed, say, the QModelIndex index (or const QModelIndex &index) of one cell, like the partId (leftmost column 0) of some row and you wish to access the other columns in that row you might write

              kitId = index.model()->index(index.row(), 1).data();
              

              Or, if you want to shorten this one, there is also a QModelIndex QModelIndex::siblingAtColumn(int column) const method, so

              kitId = index.siblingAtColumn(1).data();
              

              None of this makes a big difference which way you write it. No matter which approach you take the code will take relatively much longer fetching the data (the actual data() call in each case) from somewhere in the model than the bit of code fiddling with index.

              1 Reply Last reply
              3

              • Login

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