PDA

View Full Version : Right-Click menu on a pushButton doesn't show up



qtpyqt
3rd September 2010, 17:09
Hi,

I'm trying to add a right-click menu on a pushButton, but the menu doesn't show up??
Anyone to help?


def addChannelsMenu(self):
chmenu = QtGui.QMenu()
chmenu.addAction('testing')
chmenu.addAction('testing1')
chmenu.addSeparator()
chmenu.addAction('testing2')
#self.channelsBtn.setMenu(chmenu)

#self.channelsBtn.showMenu()
self.zoomBtn.setMenu(chmenu)



I tried repainting but it wouldn't help either:

self.zoomBtn.repaint()

I'm on Fedora8 at the moment.
:confused:

wysota
4th September 2010, 07:15
setMenu() doesn't set a context menu. If you want a context menu, you have three choices - either reimplement the contextMenuEvent() method of the button or catch the customContextMenuRequested() signal or add actions to the button and set the context menu policy accordingly (each of the solutions needs its own setting of that policy).

qtpyqt
4th September 2010, 09:46
thanks for hints wysota,
Any chance you had some examples of one of the approaches?
Looking at the documentation http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/qpushbutton.html#setMenu
"ssociates the popup menu menu with this push button. This turns the button into a menu button"
there should be a menu popping but it doesn't - do you know what I am missing here?:confused:

wysota
5th September 2010, 08:12
Any chance you had some examples of one of the approaches?
There are numerous examples in the docs and standalone Qt examples distributed with Qt itself.


Looking at the documentation http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/qpushbutton.html#setMenu
"ssociates the popup menu menu with this push button. This turns the button into a menu button"
there should be a menu popping but it doesn't - do you know what I am missing here?:confused:
You are missing the fact that the menu is not a context menu - it is not triggered with right mouse button but with a left one.

qtpyqt
5th September 2010, 10:35
I will dig into the docs to find examples, thx.
Although one thing still confuses me - if that menu was supposed to show up after left-click it still didn't :/
Got an idea why?

cheers

wysota
5th September 2010, 10:40
Maybe it went out of scope and got deleted.

qtpyqt
9th September 2010, 17:58
yepp - I just missed to parent the menu to the QButton and it's correct - the menu doesn't pop up as a right-click menu - it actually turns the whole button into a menu button.
cheers:cool: