2013-08-01 53 views
0

我試圖建立過這個例子中,我已經跨越來自這裏: Right click contextMenu on QPushButton蟒蛇PyQt4中文本菜單上的多個按鈕

我將如何得到這個當我動態創建按鈕的工作?我無法弄清楚動態創建方法on_context_menu的方法。

這是我到目前爲止的代碼。

import sys 
from PyQt4 import QtGui,QtCore 
import sip 

class LayoutTest(QtGui.QWidget): 
    def __init__(self): 
     super(LayoutTest, self).__init__() 
     self.setGeometry(300, 300, 400, 200) 
     VmasterLayout = QtGui.QVBoxLayout(self) 
     self.Hbox = QtGui.QHBoxLayout() 

     for i in range(1,4): 
      self.butVal = 'buttonMenu_%s' % i 
      self.button = QtGui.QPushButton(self.butVal) 
      self.button.clicked.connect(self.allCheckButton) 
      self.button.setContextMenuPolicy(QtCore.Qt.CustomContextMenu) 
      self.connect(self.button, QtCore.SIGNAL('customContextMenuRequested(const QPoint&)'), self.on_context_menu) 

      # create context menu 
      self.popMenu = QtGui.QMenu(self) 
      action = QtGui.QActionGroup(self, exclusive=True) 
      listVer = ['image_v001','image_v003','image_v012','image_v120','image_v140', 'image_v013'] 
      for i, vDir in enumerate(sorted(listVer)): 
       x = action.addAction(QtGui.QAction(vDir, self, checkable = True)) 
       x.triggered.connect(self.foo(x.text())) 
       self.popMenu.addAction(x) 
       self.popMenu.addSeparator() 

      self.Hbox.addWidget(self.button) 

     VmasterLayout.addLayout(self.Hbox) 

    def on_context_menu(self, point): 
     # show context menu 
     self.popMenu.exec_(self.button.mapToGlobal(point)) 

    def foo(self, name): 
     def poo(): 
      print 'Image version is: %s' % name 
     return poo 

    def allCheckButton(self): 
     point = QtGui.QCursor.pos() 
     print point 

def run(): 
    app = QtGui.QApplication(sys.argv) 
    ex = LayoutTest() 
    ex.show() 
    sys.exit(app.exec_()) 

if __name__ == "__main__": 
    run() 

回答

1

我改寫了這段代碼:python pyqt4 contextMenu on pushButton

首先,如果你想動態創建它們,你需要能夠識別不同的按鈕,所以我創建了我自己的QPushButton類 - MyPushButton

之後,我已將該類中的on_context_menu移動。

;第三重要的事情是,我已經連接MainForm類和MyPushButtonbuttonXclickedSignal信號,即給出了按鈕被點擊,因此您可以處理按鈕點擊MainForm信號和做任何你想做的不同按鈕MainForm類信息。

這裏是代碼(運行它,並嘗試點擊按鈕和按鈕上單擊鼠標右鍵):

import sys 
from PyQt4 import QtGui, QtCore 

class MyPushButton(QtGui.QPushButton): 
    def __init__(self, popMenu,elementID, mainForm): 
     super(MyPushButton, self).__init__() 
     self.__elementID = elementID 
     self.__mainForm = mainForm 
     self.__popMenu = popMenu 

     self.connect(self, QtCore.SIGNAL('customContextMenuRequested(const QPoint&)'), self.on_context_menu) 
     self.connect(self, QtCore.SIGNAL('clicked()'), self,  QtCore.SLOT("triggerOutput()"))  

    def on_context_menu(self, point): 
     # show context menu 
     self.__popMenu.exec_(self.mapToGlobal(point)) 

    @QtCore.pyqtSlot() 
    def triggerOutput(self): 
     self.__mainForm.emit(QtCore.SIGNAL("buttonXclickedSignal(PyQt_PyObject)"), self.__elementID) # send signal to MainForm class 


class MainForm(QtGui.QWidget): 
    def __init__(self, parent=None): 
     super(MainForm, self).__init__(parent) 
     self.setGeometry(300, 300, 400, 200) 
     VmasterLayout = QtGui.QVBoxLayout(self) 
     self.Hbox = QtGui.QHBoxLayout() 

     # Custom signal 
     self.connect(self, QtCore.SIGNAL("buttonXclickedSignal(PyQt_PyObject)"),   self.buttonXclicked) 

     for i in range(1,4): 
      # create context menu as you like 
      popMenu = QtGui.QMenu(self) 
      popMenu.addAction(QtGui.QAction('button %s - test0'%(i), self)) 
      popMenu.addAction(QtGui.QAction('button %s - test1'%(i), self)) 
      popMenu.addSeparator() 
      popMenu.addAction(QtGui.QAction('button %s - test2'%(i), self)) 

      # create button 
      self.button = MyPushButton(popMenu, i, self) 
      self.button.setText("test button %s" %(i))  
      self.button.resize(100, 30) 

      # set button context menu policy 
      self.button.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)   

      self.Hbox.addWidget(self.button) 

     VmasterLayout.addLayout(self.Hbox) 

    def buttonXclicked(self, buttonID): 
     if buttonID == 1: 
      #do something , call some method .. 
      print "button with ID ", buttonID, " is clicked" 
     if buttonID == 2: 
      #do something , call some method .. 
      print "button with ID ", buttonID, " is clicked" 
     if buttonID == 3: 
      #do something , call some method .. 
      print "button with ID ", buttonID, " is clicked" 

def main(): 
    app = QtGui.QApplication(sys.argv) 
    form = MainForm() 
    form.show() 
    app.exec_() 

if __name__ == '__main__': 
    main()