#customContextMenu trigger
#This is the example code for customContextMenu trigger
#If your are not expecting this answer, sorry.
import sys
from PyQt4 import QtGui
from PyQt4 import QtCore
class Window (QtGui.QWidget):
def __init__(self, parent=None):
super(Window, self).__init__(parent)
self.listWidget = QtGui.QListWidget(self)
self.listWidget.setObjectName('listWidget')
self.pushButton = QtGui.QPushButton(self)
self.pushButton.setGeometry(QtCore.QRect(20, 220, 101, 23))
self.pushButton.setObjectName('pushButton')
self.pushButton.setText('Add')
self.verticalLayout = QtGui.QVBoxLayout(self)
self.verticalLayout.setObjectName ('verticalLayout')
self.verticalLayout.addWidget(self.listWidget)
self.verticalLayout.addWidget(self.pushButton)
#Right click menu
self.listWidget.setContextMenuPolicy (QtCore.Qt.CustomContextMenu)
self.listWidget.customContextMenuRequested.connect (self.rightClickFunction)
self.action = QtGui.QAction (self)
self.action.setObjectName('action')
self.action.setText ('Open')
self.action1 = QtGui.QAction (self)
self.action1.setObjectName('action1')
self.action1.setText ('Test')
self.customMenu = QtGui.QMenu('Menu', self.listWidget)
self.customMenu.addAction (self.action)
self.customMenu.addAction (self.action1)
#self.customMenu.addAction (QtGui.QIcon(''), 'Open', (self.oepnFunction))
#self.customMenu.addAction (QtGui.QIcon(''), 'Test', (self.testFunction))
self.pushButton.clicked.connect (self.addItem)
self.action.triggered.connect (self.oepnFunction)
self.action1.triggered.connect (self.testFunction)
#void changed()
#void hovered()
#void toggled (bool)
#void triggered (bool = 0)
def addItem (self) :
count = int (self.listWidget.count())
self.listWidget.addItem (str(count+1) + '_')
def rightClickFunction (self, event) :
index = self.listWidget.indexAt (event)
if not index.isValid():
return
item = self.listWidget.indexAt(event)
self.customMenu.popup (QtGui.QCursor.pos())
def oepnFunction (self) :
print 'hai............open'
def testFunction (self) :
print 'hai............test'
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
w = Window()
w.show()
sys.exit(app.exec_())
這是什麼平臺?你描述的行爲是Linux上的默認行爲。 – ekhumoro