2016-07-28 88 views
0

我想改變一個自定義contextMenu如何觸發一個小部件,但我還沒有找到解決方案。我在一個標準的方式添加一個ContextMenu到QListWidget:PyQt:我如何更改customContextMenu觸發器?

self.shotsList = QtGui.QListWidget() 
self.shotsList.setContextMenuPolicy(QtCore.Qt.CustomContextMenu) 
self.shotsList.customContextMenuRequested.connect(self.shotsPopUp) 

用戶請求菜單顯示在鼠標右鍵按下(而不是鼠標釋放),那麼他們可以選擇菜單項,這是在發佈時觸發(標記菜單樣式)。

這可能嗎?

+0

這是什麼平臺?你描述的行爲是Linux上的默認行爲。 – ekhumoro

回答

0
#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_())