QMenu with a additional QPushButton
Hello,
I also asked this question in another forum, but I haven't got a satisfying answer. I have a simple QMenu with several items. For a few of them I want to add an additional QPushButton to the right side to open an options dialog.
Like in this picture (from Silo 2.2 which also uses Qt):
http://img40.imageshack.us/img40/7707/twobuttonmenu.jpg
How can I achieve this?
Re: QMenu with a additional QPushButton
I would subclass QMenu, and in the paintEvent of the customised class try to figure out the right place for the buttons and display them.
Re: QMenu with a additional QPushButton
You can use QWidgetAction class and subclass it it add you own widget to you menu actions.
Like this, implement a custom action PushButtonAction
Code:
{
public:
explicit PushButtonAction
(const QString
& action_text,
QObject* parent
= 0);
virtual ~PushButtonAction();
private:
};
PushButtonAction
::PushButtonAction(const QString
& action_text,
QObject* parent
) , text(action_text)
{
layout
->addWidget
(new QLabel(text
));
widget->setLayout(layout);
setDefaultWidget(widget);
}
and while creating menu
Code:
menu->addAction(tr("Action&1"));
menu->addAction(tr("Action&2"));
menu->addSeparator();
menu->addAction(new PushButtonAction("My Action"));
menu->addAction(tr("Action&3"));
menu->addSeparator();
menu->addAction(tr("Action&4"));
menu->addAction(tr("Action&5"));
There is a problem with this method, you will have to sacrifice the QAction default features, like checkable, icon, selection highlight, mouse hove, etc.. all these have to be reimplemented in your custom widget again. If you plan to use all custom actions in your menu, then it not a bad idea to implement those.
Re: QMenu with a additional QPushButton
I wouldn't implement something like that as a QMenu. More likely as a widget, be it a custom one based on QAction (to be compliant with QMenu approach) or simply a QTreeWidget/QTableWidget. Menus have a well defined and well known behaviour and trying to change that behaviour hits the user experience and intuition of your application.