Reputation: 341
I have created a custom menu as a Popup. When I use addAction the items are arranged vertically. Is it possible to align the QMenu items horizontally?
Upvotes: 1
Views: 1124
Reputation: 4050
Yeah, it's posible. You can implement your own QWidgetAction and add QToolButton in an horizontal layout do display the action:
class MyAction : public QWidgetAction
{
public:
MyAction( QObject * parent ) :QWidgetAction(arent) {}
void releaseWidget(QWidget * widget) {widget->deleteLater();}
QWidget * requestWidget ( QWidget * parent ) {
//Then implement your own widget
QWidget* widget = new QWidget(this);
widget->setLayout(new QHBoxLayout(wdiget));
widget->layout()->addWidget(new QToolButton("Example"));
return widget;
}
};
Other way its to set up the widget manually without subclassing:
QWidgetAction * wa = new QWidgetAction(this);
wa->setDefaultWidget(new QPushButton("Default")); // Example a pushbutton
And then use it in your QMenu, you can add other actions or different widgets:
MyWidgetAction *mwa = new MyWidgetAction(this); //3
ui->menuBar->addAction( mwa ); //3 - noop. nothing added to menu bar
Upvotes: 2