2

I'm using PyQt4 and python 2.7.9. I have a QTreeView that contains data from Oracle database. the code is this:

model = QStandardItemModel(0,1)
self.treeMedia.setModel(model)
for rowalb in self.SELECT_TREE_ALBUM(codus):
    nodeItem = QStandardItem(str(rowalb[1]).decode('utf-8'))
    for rowph in self.SELECT_TREE_PHOTO(int(rowalb[0])):
        childItem = QStandardItem(str(rowph[0]))
        childItem.setEditable(False)
        nodeItem.insertRows(0, [childItem])
    nodeItem.setEditable(False)
    model.appendRow(nodeItem)
model.setHorizontalHeaderLabels(['Data'])

The SELECT_TREE_ALBUM (codus is the id of the album's owner) and SELECT_TREE_PHOTO are the functions that return data from database.The rowalb 1 is the name of the album and rowalb[0] is the ID, the rowalb[0] is used to get photos of this album.The picture shows this view:

enter image description here

I want to get the childItem data (e.g. 1491475964461012, 1491475821127693, 1491475631127712, 1491475141127761 or 1480407552234520) using doubleclick event. I tried to put these code into the constructor:

self.treeMedia.doubleClicked.connect(self.treeMedia_doubleClicked)

and after that I added the function:

def treeMedia_doubleClicked(self,index):
    item = self.treeMedia.model().item(index.row(),index.column())
    strData = item.data(0).toPyObject()
    #self.treeMedia.currentIndex()
    print('' + str(strData))

but sometimes I see the nodeItem information (e.g. "terror" or "fotos de perfil") and other ones I get this error: "AttributeError: 'NoneType' object has no attribute 'data'".What could be the problem? is the problem in filling the qtreeview? or is the problem in double click event function when using the index? Please help me. Thanks in advance.

GSandro_Strongs
  • 773
  • 3
  • 11
  • 24

2 Answers2

12

If anyone is looking for answer.

def treeMedia_doubleClicked(self,index):
    item = self.treeView.selectedIndexes()[0]
    print item.model().itemFromIndex(index).text()
Svilen Minkov
  • 136
  • 2
  • 4
  • Thanks a lot It's ok. I was looking for the solutions for many time – GSandro_Strongs Oct 13 '15 at 15:43
  • This might not be the correct solution for example consider the case of multiple selections, there has to be a way to use the index. – JKAbrams Sep 12 '20 at 21:26
  • That is a bizarre and unnecessary way to get the model! You started with the treeview but then get the `QModelIndex` of the first selected item and call `model()` on it. That is pointlessly indirect and there might not even be any selected items (though that is tricky when double clicking) in which case it would crash. You could just use `self.treeview.model()`, or even `index.model()` (`index` is also already a `QModelIndex`!). – Arthur Tacca Aug 20 '21 at 21:26
  • @JKAbrams It does actually use `index` to do the indexing, the `selectedIndices()[0]` is just get the model to index into (albeit not the best way to do that). – Arthur Tacca Aug 20 '21 at 21:27
0

I realize this is a bit out of context, but posting here for any other passerbys...

in pyside6 either of these work

def treeMedia_doubleClicked(self, index):
    print(self.name_of_model.filePath(index))

or

def treeMedia_doubleClicked(self, index):
    print(index.model().filePath(index))
joshp
  • 706
  • 5
  • 22