0

With QPushButton you can add a menu using the setMenu() method and it will then draw a down arrow on the button (depending on the style settings).

Is it possible to enable the drawing of the same down arrow by some other method? I would like to use this style of button to show a custom popup widget when it is clicked.

glennr
  • 2,069
  • 2
  • 26
  • 37
  • How about implementing `myCustomWidgetAction` like @Nejat suggested and then adding that widget action into a `QMenu`, which then is passed to `QPushButton` in `setMenu()`? – Googie Jul 25 '14 at 06:53

2 Answers2

0

You can use a QToolButton instead of QPushButton and add actions to the QToolButton. 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 toolButton to be displayed in a popup menu:

myCustomWidgetAction * widgetAction   = new myCustomWidgetAction(this);

ui->toolButton->addAction(widgetAction);

Your custom widget can be a list containing different elements or it can be any other widget. You can also add multiple instances of myCustomWidgetAction to the toolButton.

Nejat
  • 31,784
  • 12
  • 106
  • 138
  • I didn't want to do that because on OSX QToolButton looks different to QPushButton, it has square corners. I know I can change this using a stylesheet but then I would have to recreate the styles for all of the button states. – glennr Jul 24 '14 at 20:41
  • I tried this but using a QPushButton with a menu and adding the CustomWidgetAction to the menu as suggested by @Googie, but I couldn't get it to work as both the menu and my popup get shown. I was previously able to get the popup to get drawn in the menu by deriving my popup from QWidgetAction but was unable to figure out how to change the menu size so that the popup was not obscured or the style of the menu to remove the border and rounded corners. – glennr Jul 25 '14 at 20:41
0

What I ended up doing was adding an empty menu to the QPushButton to make it show the down arrow and then showing my popup widget on the aboutToShow() signal. The only issue with this is that the popup doesn't get the focus unless you click in it, which can be worked around by simulating a mouse click as described here.

Community
  • 1
  • 1
glennr
  • 2,069
  • 2
  • 26
  • 37
  • That's a tricky solution. I suppose you'd better subclass QPushButton reimplementing paintEvent to draw the dropdown arrow and mousePressed to handle clicking on the arrow... – matthieu Jul 01 '18 at 21:24