PDA

View Full Version : How can I get data from the hidden column in a QTreeView?



serj
3rd April 2015, 15:33
Hi, everyone! I have a qtreeview, that displays some information from the sqlite database. When I click on the item in the tree, I go to the DB and trying to find details about selected item... The trouble is that i need to search information in the DB by ID, not by displayed name.. So, I decided to draw additional column in the tree that will hold the ID. When I click on the row, I take element's ID with help of self.ui.treeView.selectedIndexes()[1] (0 column is a name)
But. When I am trying to hide column using hideColumn(), i get an "index is out of bounds" error... In the description of selectedIndexes() I read that "This convenience function returns a list of all selected and non-hidden item indexes in the view."... So, is there another way to tackle with my problem?

Thanks!

anda_skoa
3rd April 2015, 16:04
You take the index for the first column and then ask the model for the index of the second column, using the parent index and row of the index you have.

Cheers,
_

d_stranz
3rd April 2015, 18:04
Alternatively, you can store the ID as Qt::UserRole data for the model index: QAbstractItemModel::setData(). When you get notified of the selected index, you ask it for the UserRole data, which is returned as a QVariant.

serj
3rd April 2015, 21:36
Oh, I tried to do what you described.. It works only while my column is visible...

hm... I'll try it, thanks!

d_stranz
3rd April 2015, 21:40
Are you using a proxy model to convert the DB result into something to display in the tree?

serj
3rd April 2015, 21:59
I am creating QStandardItem and recursively appending rows..

child_node = QtGui.QStandardItem("ITEM TEXT")
root.appendRow(child_node)

"ITEM TEXT" - is getting directly from sqlite...for each iteration...

d_stranz
4th April 2015, 03:52
Well then it is easy - when you create the QStandardItem, just call
child_node.setData( QVariant( ID ), Qt::UserRole ) When you get the selected item, then it is just
ID = selected_node.data( Qt::UserRole ) (or whatever the appropriate syntax is - I'm not a python programmer).

serj
4th April 2015, 08:18
It works! Thanks! This is exactly what I need..

child_node.setData(QtCore.QVariant(str(chld["ID"]))) #setting data

selected_id = str(index.model().itemFromIndex(index).data().toSt ring()) #getting data