how to get the pyqt qtreeview item child using double click event?

GSandro_Strongs picture GSandro_Strongs · Apr 16, 2015 · Viewed 7.9k times · Source

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.

Answer

Svilen Minkov picture Svilen Minkov · Sep 24, 2015

If anyone is looking for answer.

def treeMedia_doubleClicked(self,index):
    item = self.treeView.selectedIndexes()[0]
    print item.model().itemFromIndex(index).text()