There are rows in QTreeView, each row has two columns. The first column carries a name, second column carries a preview.
Questions:
How to get click on row signal? If row is selected by single mouse click, how to get signal? I use
QTreeView.doubleClicked.connect()now, but is there way how to do?I follow up on the first question. If I choose by clicking on the item in the second column how to get item name from the first column? (if is not possible to perform the action with a single mouse click, how to do it with
QTreeView.doubleClicked.connect()? )How to get information about category, subcategories, row, column of item?
Thanks for hints and help.
Code preview:
- QTreeView
self.tree = QtWidgets.QTreeView()
self.model = QtGui.QStandardItemModel(self.tree)
self.tree.setDragEnabled(True)
self.tree.doubleClicked.connect(self.getValue)
- Filling QTreeView
self.model.setHorizontalHeaderLabels(('Category / Subcategories / Name', 'Preview'))
self.tree.setModel(self.model)
self.tree.setColumnWidth(0, 200)
#root = self.tree.rootIndex()
for row, (keys, vals) in enumerate(self.dataStatObj.items()):
if row == 0:
kategorie = keys[0]
podkategorie = keys[1]
categoryTV = QtGui.QStandardItem(keys[0])
subcategoriesTV = QtGui.QStandardItem(keys[1])
self.model.appendRow(categoryTV)
for iimg in listImagesNameSuffix:
if iimg[1] == keys[2]:
print(iimg[1])
print(keys[2])
imgName = iimg[0]
imgTV = StandardItemObr()
imgTV.setIcon(QtGui.QIcon(pathToImages + imgName))
imgTV.setEditable(False)
nameImgTV = QStandardItem(keys[2])
nameImgTV.setEditable(False)
categoryTV.appendRow(subcategoriesTV)
categoryTV.setEditable(False)
subcategoriesTV.appendRow([nameImgTV, imgTV])
subcategoriesTV.setEditable(False)
...
...
self.tree.expandAll()
self.tree.setIconSize(QtCore.QSize(100, 100))
- getValue (
self.tree.doubleClicked.connect(self.getValue))
def getValue(self, val):
print(val.data(), 'If I click on the first column, the text will appear.'
' When I click on the second column I need to return the same as for the first column ')
if val.data() is None:
print('None')
print(val.parent(), "PARENT")
- Preview
