You can use QWidgetAction class and subclass it it add you own widget to you menu actions.
Like this, implement a custom action PushButtonAction
{
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);
}
class PushButtonAction : public QWidgetAction
{
public:
explicit PushButtonAction(const QString& action_text, QObject* parent = 0);
virtual ~PushButtonAction();
private:
QString text;
};
PushButtonAction::PushButtonAction(const QString& action_text, QObject* parent)
: QWidgetAction(parent)
, text(action_text)
{
QWidget* widget = new QWidget();
QHBoxLayout* layout = new QHBoxLayout();
layout->addWidget(new QLabel(text));
layout->addWidget(new QPushButton("Open Dialog"));
widget->setLayout(layout);
setDefaultWidget(widget);
}
To copy to clipboard, switch view to plain text mode
and while creating menu
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"));
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"));
To copy to clipboard, switch view to plain text mode
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.
Bookmarks