View Single Post
marxian's Avatar
Posts: 2,448 | Thanked: 9,523 times | Joined on Aug 2010 @ Wigan, UK
#5
I have been able to achieve what I think you are looking for, though I am using a QListWidget, rather than a QListView.

To obtain a context menu following a long press, I first set the contextMenuPolicy:

Code:
self.listWidget.setContextMenuPolicy(Qt.ActionsContextMenu)
Then I add some QActions to the listWidget, e.g:

Code:
self.actionEdit = QAction("Edit", self.listWidget)
self.actionDelete = QAction("Delete", self.listWidget)
self.listWidget.addAction(self.actionEdit)
self.listWidget.addAction(self.actionDelete)
Now, when you long-press an item in the QListWidget/View, the context menu containing the two QActions will appear. You can obtain the currently selected row using the currentRow() method, e.g:

Code:
self.connect(self.actionEdit, SIGNAL("triggered()"), self.testMethod)

def testMethod(self):
row = self.listWidget.currentRow() print row