PDA

View Full Version : How to get data from TableView - QML Desktop Components



travick
17th January 2013, 12:43
Anyone has an idea how to get data from currently selected item from TableView?
In ListView there is currentItem, but in TableView there is only currentIndex. Index should be enough, just get data at index. But how do do that? It should be extremely simple.

I'd like to get item and show additional info (description) in separate text area.

Any ideas?

wysota
17th January 2013, 14:34
Hmmm... by getting the index number from currentIndex.row ?

travick
23rd January 2013, 18:35
currentIndex itself is row index so I have row index but still no idea how to get object at this index. I can get it from python backend, but I don't want to - unnecessary redundant code.
If I have ListView there is currentItem and I'm able to operate directly on this item.

wysota
23rd January 2013, 18:55
Doesn't this work?

model.get(currentIndex).someRole

travick
23rd January 2013, 20:25
TypeError: Result of expression 'model.get' [undefined] is not a function.

The same with model[currentIndex]

TypeError: Result of expression 'model[currentIndex]' [undefined] is not an object.

wysota
23rd January 2013, 21:02
Is model a "ListModel" or some other kind of model?

travick
23rd January 2013, 21:06
In python implementation of AbstractListModel.
Of course all data from model appears correctly.

wysota
23rd January 2013, 21:14
If it's a QAbstractItemModel then you need to provide your own method for accessing data from the model.

I suggest an equivalent of the following:

QVariantMap Model::get(int idx) const {
QVariantMap map;
foreach(int k, roleNames().keys()) {
map[roleNames().value(k)] = data(index(idx, 0), k);
}
return map;
}

travick
23rd January 2013, 21:31
Thanks a lot, it works!