How can I add custom widget as Popup menu for ToolButton?

1.7k Views Asked by At

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?

1

There are 1 best solutions below

1
On BEST ANSWER

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.