Is there a way to add a QWidget to a QMenu in QtCreator
Asked Answered
C

3

11

I'm creating a text editor and I'd like to put the QComboBox in the QMenu. I didn't find any method inside the QMenu that handled such a thing. The closest is QMenu::addAction(). I was wondering of getting around this hurdle.

Thanks!

Calendar answered 2/12, 2011 at 15:55 Comment(0)
V
19

You have to subclass QWidgetAction and then simply call the addAction to your menu.

Example code for Spin Box Action with a label

class SpinBoxAction : public QWidgetAction {
public:
    SpinBoxAction (const QString& title) : 
      QWidgetAction (NULL) {
        QWidget* pWidget = new QWidget (NULL);
        QHBoxLayout* pLayout = new QHBoxLayout();
        QLabel* pLabel = new QLabel (title);  //bug fixed here, pointer was missing
        pLayout->addWidget (pLabel);
        pSpinBox = new QSpinBox(NULL);
        pLayout->addWidget (pSpinBox);
        pWidget->setLayout (pLayout);

        setDefaultWidget(pWidget);
    }

    QSpinBox * spinBox () {
        return pSpinBox;
    }

private:
    QSpinBox * pSpinBox;
};

Now simply create it and add it to your menu

SpinBoxAction * spinBoxAction = new SpinBoxAction(tr("Action Title"));
// make a connection
connect(spinBoxAction ->spinBox(), SIGNAL(valueChanged(int)), 
        this, SLOT(spinboxValueChanged(int)));
// add it to your menu
menu->addAction(spinBoxAction);
Vair answered 2/12, 2011 at 16:23 Comment(5)
Why would you prefer subclassing QWidgetAction? Wouldn't it offer looser coupling and more maintainable code to instantiate a QWidgetAction separately from your pWidget and call setDefaultWidget? Wouldn't the only reason to subclass be to implement createWidget?Nix
It depends. If you want to have reusable widget actions and avoid rewriting the same parts of code for creating them you should subclass them. For example I once needed a QMenu with multiple spin boxes with a label, and varying min/max values and prefix. By subclassing the QWidgetAction I was able to have a reusable element, and in every case I only had to change the constructor arguments. Also it is straight forward to create a small library with action widgets and just call the one you need when you need it.Vair
I'm not familar with this kind of constructor:SpinBoxAction (const QString& title) : QWidgetAction (NULL) {}Calendar
This simply calls the parent's constructor with NULL as argument. #121376Vair
@MagnusHoff So why would you need to implement createWidget?Moreover
N
2

QWidgetAction is a QAction that contains a QWidget. You can use this to encapsulate your QComboBox and add it to your menu via QMenu::addAction.

Nix answered 2/12, 2011 at 16:23 Comment(0)
Z
1

You can always use a QWidget or QFrame as the Menu Widget, then put a QHBoxLayout on it, and insert your QWidgets inside.

Zap answered 2/12, 2011 at 16:12 Comment(1)
This idea actually occured to me. I'm kinda new to Qt so forgive me if this is kinda basic: How would I set my own QWidget as the Menu Widget.Calendar

© 2022 - 2024 — McMap. All rights reserved.