水平排列QMenu项目

时间:2017-07-10 14:10:47

标签: c++ qt qmenu

我已经创建了一个自定义菜单作为弹出窗口。当我使用addAction时,项目是垂直排列的。是否可以水平对齐QMenu项目?

1 个答案:

答案 0 :(得分:1)

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