How can I get data from the hidden column in a QTreeView?
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!
Re: How can I get data from the hidden column in a QTreeView?
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,
_
Re: How can I get data from the hidden column in a QTreeView?
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.
Re: How can I get data from the hidden column in a QTreeView?
Oh, I tried to do what you described.. It works only while my column is visible...
hm... I'll try it, thanks!
Re: How can I get data from the hidden column in a QTreeView?
Are you using a proxy model to convert the DB result into something to display in the tree?
Re: How can I get data from the hidden column in a QTreeView?
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...
Re: How can I get data from the hidden column in a QTreeView?
Well then it is easy - when you create the QStandardItem, just call
Code:
child_node.
setData( QVariant( ID
), Qt
::UserRole )
When you get the selected item, then it is just
Code:
ID = selected_node.data( Qt::UserRole )
(or whatever the appropriate syntax is - I'm not a python programmer).
Re: How can I get data from the hidden column in a QTreeView?
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