I have created a custom Widget,it has to be displayed as a popup menu when click on the ToolButton. How I can do this in Qt 5.1.1
?
Asked
Active
Viewed 1,613 times
1 Answers
7
You should create your custom QWidgetAction
to add to the popup menu.
This is a sample QWidgetAction
:
#include <QWidgetAction>
class myCustomWidgetAction: public QWidgetAction
{
Q_OBJECT
public:
explicit myCustomWidgetAction(QWidget * parent);
protected:
QWidget * createWidget(QWidget *parent);
};
myCustomWidgetAction::myCustomWidgetAction(QWidget * parent):QWidgetAction(parent) {
}
QWidget * myCustomWidgetAction::createWidget(QWidget *parent){
myCustomWidget * widget=new myCustomWidget(parent);
return widget;
}
You can then add your widget to the tool button to be displayed in a popup menu:
myCustomWidgetAction * widgetAction = new myCustomWidgetAction(this);
ui->toolButton->addAction(widgetAction);
myCustomWidget
can be any widget. You can add multiple instances of myCustomWidgetAction
to the toolButton.

Nejat
- 31,784
- 12
- 106
- 138
-
@SureshR If it's working fine, then mark the answer as accepted. And start accepting all the answers that contributed to solving your other problems/questions. Else chanses are nobody will help you next time you will have a problem ;) – Iuliu Nov 19 '14 at 12:49