python pyqt4 contextMenu on multiple pushButtons -
i'm trying build off example i've came across here: right click contextmenu on qpushbutton
how work when i'm creating button dynamically? can't figure out way dynamically create method on_context_menu
.
here code have far.
import sys 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() in range(1,4): self.butval = 'buttonmenu_%s' % 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'] 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()
i've rewrite code: python pyqt4 contextmenu on pushbutton.
first of all, need able identify different buttons if want create them dynamically, i've created own qpushbutton
class - mypushbutton
.
after i've moved on_context_menu
in class.
and third important thing i've connected mainform
class , mypushbutton
class buttonxclickedsignal
signal , gives mainform
class information button clicked can handle button clicked signal in mainform
, whatever want different buttons.
here code (run , try click buttons , right click on buttons):
import sys 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 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) in range(1,4): # create context menu 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 , call method .. print "button id ", buttonid, " clicked" if buttonid == 2: #do , call method .. print "button id ", buttonid, " clicked" if buttonid == 3: #do , call method .. print "button id ", buttonid, " clicked" def main(): app = qtgui.qapplication(sys.argv) form = mainform() form.show() app.exec_() if __name__ == '__main__': main()
Comments
Post a Comment