PDA

View Full Version : 2 ways to get data from model



Higgs
22nd February 2014, 18:30
Hi all.
I'm learning from Johan Thelin's book(based on Qt 4.x) and there is written that,to get data from model we use:


int value2 = index.model()->data(index,Qt::DisplayRole).toInt();


But in documentation(Qt 5.1.1) there is another way:


int value = index.data().toInt();


Both work for me very well. But what is the difference? Which one I should use?

Infinity
23rd February 2014, 03:44
I would use the second version because it is shorter. You don't need to use the index twice. Apart from that there's not much difference.
There are also a lot of other convenience functions all over the Qt framework.

anda_skoa
23rd February 2014, 12:53
Ultimately all calls for data end up in the model's data() method.

As Infinity wrote, QModelIndex::data() is just a short hand for getting the pointer to the model and calling its data() method.

Cheers,
_