PDA

View Full Version : Subclass Pushbutton



Smith
6th December 2007, 21:55
Hi Qt Community,

I like to right mouse click on a Push Button to select a context menu or popup menu.
However, I like to left mouse click for the Push Button, using the click() signal.

This would combine the Push Button and Popup Menu Button into one button, selecting the Pushbutton with the left mouse button and the right mouse button for the Popup Menu Button or context menu.

Please point me in the right direction, lost.

Thanks,

Newbie:confused:

wysota
6th December 2007, 22:37
Hi Qt Community,
Hi Qt Fan!


Please point me in the right direction, lost.
Reimplement QWidget::contextMenuEvent ( QContextMenuEvent * e ).

jpn
7th December 2007, 06:53
Hi, have you noticed QPushButton::setPopup()?

Smith
7th December 2007, 10:46
Thanks for the advice, I am still struggling.

The Qt menu example for QWidget::contextMenuEvent ( QContextMenuEvent * e ) has the main window as the parent. If you right mouse click the context menu is display any where on the main window. I would like a context menu for a specific pushbutton. I am unsure how to make the pushbutton the parent of the context window.

Using the popup option seems to make the button a dedicated popup menu button.

I am using the example from the Qt Quarterly,

http://doc.trolltech.com/qq/qq10-signalmapper.html

Each button pass a integer value to a slot for a QProcess. I would like to add three more slots and connect these slots to a context menu, passing a integer value.

I really have two problems to resolve, pushbutton context menu and passing integer to the selected slot.

Thanks for your help,

Newbie

wysota
7th December 2007, 10:52
I admit I don't understand what your problem is... QPopupMenu takes a pointer to a widget (any widget) as a parent and you can always pass 0 there, so that's not a problem. The main idea of this approach is to create a QPopupMenu object and call its exec() method to show it on screen at a desired position. I think the example is pretty clear at what needs to be done.

Smith
8th December 2007, 06:59
Thanks for the advice, I am able to get the popup menus. I use a array of pushbuttons and need to make array of popup menus for this to work. I believe I may need to pass a integer to the contextMenuEvent( QContextMenuEvent * ) function. However, the ContextMenuEvent member does not have any signals which can be used with the connect or signal mapper. Any advice or I am still missing something.

Newbie:confused:

wysota
8th December 2007, 12:56
I suggest you subclass QPushButton and introduce a method to associate a popup menu with each instance of the button and then use the subclass instead of QPushButton on your forms or widgets. You can use the promotion feature of Designer to insert your subclassed widgets instead of QPushButtons on a Designer form. It'll be much easier this way than using a mapper.