如何用Qt的designer添加菜单栏

如何用Qt的designer添加菜单栏,第1张

在使用Qt创建桌面程序的时候,经常会使用Qt designer来设计程序界面。而Qt窗口类中,QMenuBar和QToolBar中并没有按钮或选项类的控件,取而代之的是要向这些控件中添加QAction。这一个个Action构成了类似按钮似的“控件”。网上很多在源代码中添加QAction的方法。实际上,在Qt designer中也可以方便的想菜单栏和工具栏添加action。方法如下:1、打开动作编辑器。如果找不到动作编辑器,请点击Qt designer的菜单栏的“视图”-“动作编辑器”。2、在动作编辑器窗口添加新的动作3、拖动该动作到菜单栏或状态栏大功告成如果想为改Action设置响应函数,可以将该Action的Triggered()信号与想要的槽函数相连。如//连接信号和槽connect(ui.actionInputFlightData,SIGNAL(triggered()),this,SLOT(testTra()))如何用Qt的designer添加菜单栏

//程序抽象类

#include <QApplication>

//窗口类

#include <QWidget>

#include <QPushButton>

#include <QLineEdit>

//自动竖版布局

#include <QVBoxLayout>

//自动横版布局

#include <QHBoxLayout>

//自动格子布局

#include <QGridLayout>

int main(int argc, char* argv[])

{

    QApplication app(argc, argv)

    QWidget w

    //按钮也是一个窗口

    QPushButton button

    button.setText("你好")

    //button.setParent(&w)

    button.show()

    //输入框也是一个窗口

    QLineEdit editor

    editor.setEchoMode(QLineEdit::Normal)

    editor.setPlaceholderText("请输入文本")

    //editor.setParent(&w)

    editor.show()

    //水平方向上的layout

    //QVBoxLayout vBox

    //vBox.addWidget(&button)

    //vBox.addWidget(&editor)

    //垂直方向上的layout

    QHBoxLayout hBox

    //设置左右d簧 使得控件大小不随窗口改变

    hBox.addStretch(1)

    hBox.addWidget(&button,1)

    //设置两个空间的间距为20

    hBox.addSpacing(20)

    hBox.addWidget(&editor,1)

    hBox.addStretch(1)

    w.setWindowTitle("hello world")

    w.show()

    w.setLayout(&hBox)

    return app.exec()

}


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/bake/11906045.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-19
下一篇2023-05-19

发表评论

登录后才能评论

评论列表(0条)

    保存