You are not logged in.

Dear visitor, welcome to QtForum.org. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

1

Wednesday, January 4th 2012, 7:50pm

How to put pushbutton inside the QMenu or QAction control?

I need to put a pushButton inside a QMenu. Is it possible and if yes then how?
I want to achieve something like this:

Junior

Professional

  • "Junior" is male

Posts: 1,622

Location: San Antonio, TX USA

Occupation: Senior Secure Systems Engineer

  • Send private message

2

Friday, January 6th 2012, 1:28pm

You might want to check into re-implementing QWidgetAction

3

Friday, January 6th 2012, 2:48pm

Yeah, I've already tried that stuff, and this is what I've got:

Here is code:

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class SpinBoxAction : public QWidgetAction {
public:
    SpinBoxAction (const QString & title) :
      QWidgetAction (NULL) {
        QWidget* pWidget = new QWidget (NULL);
        QHBoxLayout* pLayout = new QHBoxLayout();

        pLabel = new QLabel(title);
        pPushButton = new QPushButton("+");
        pMenu = new QMenu();
        pMenu->addMenu("");

        pLayout->addWidget(pLabel);
        pLayout->addWidget(pPushButton);
        pLayout->addWidget(pMenu);

        pWidget->setLayout(pLayout);

        setDefaultWidget(pWidget);
    }

    }

private:
    QMenu *pMenu;
    QPushButton *pPushButton;
    QLabel *pLabel;
};

Calling:

Source code

1
2
SpinBoxAction * spinBoxAction = new SpinBoxAction(tr("Action Title"));
    ui->menuOlolo->addAction(spinBoxAction);

This is not that result what I'm expecting, new menu item should look like native but extended, and in my case I have no icon space, and menu has a border. And, of course, button is near menu, not built in.

Similar threads

Used tags

QAction, qmenu, qt