Retrieving data from QModelIndex
-
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 ! -
@Gandalf404
Yours are equivalent, but the first one arbitrarily picks column 0 when it could more generically pickindex.column()
for that.
Your first way is overlong, second way is shorter.
ButQModelIndex
lets you access thedata()
there directly via index.data() (defaults toQt::DisplayRole
). However you will need the second way if you want to callsetData()
rather thandata()
. -
@Gandalf404
No. Like I saidwhatever = index.data()
since you already have aQModelIndex
, what is not clear?
Orwhatever = model->data(somRow, someRolumn)
if you don't happen to start with aQModelIndex
. -
@JonB Maybe you misunderstood. There is a class, that need to be set up:
Set up with data from chosen row, (part id, part count etc.):
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 ? -
@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 isindex.data()
. - If you do not have a
QModelIndex
but have the desired row/column in some variables you can usemodel.data(row, column)
.
If you have a desired
QModelIndex index
the following three expressions all return the same value:index.model()->index(index.row(), index.column()).data()
index.model()->data(index)
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 aQModelIndex 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
(orconst QModelIndex &index
) of one cell, like thepartId
(leftmost column 0) of some row and you wish to access the other columns in that row you might writekitId = 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. - If you already have a