0

I would like to show a context Menu in a QTreeWidget only for child items. For the moment, I am getting the index with the following function:

def menuItem(self,pos):
    index = self.ui.tree.indexAt(pos)

    if not index.isValid():
        return 

    menu = QtGui.QMenu(self)
    menu.addAction("Action 1")
    menu.addAction("Action 2")
    menu.exec_(QtGui.QCursor.pos())

But I would need to know if this index is a children one. Any suggestion?

myildirim
  • 2,248
  • 2
  • 19
  • 25
Ucotecnico
  • 3
  • 1
  • 3

1 Answers1

2

Top-level items will have invalid index for their parents. You can include that in your check:

if not index.isValid() or not index.parent().isValid():
    return
Avaris
  • 35,883
  • 7
  • 81
  • 72