Qt 学习第八天:菜单栏、工具栏、状态栏、模态和非模态对话框创建

首先先创建QMainwindow文件

以下代码全在MainWindow.cpp下

1. 创建菜单栏

cpp 复制代码
    //创建菜单栏
    QMenuBar *menu = menuBar();
    //创建菜单
    QMenu *fileMenu = menu->addMenu("文件");
    QMenu *editMenu = menu->addMenu("编辑");
    //创建文件中菜单项
    QAction *createPro = fileMenu->addAction("新建项目");
    fileMenu->addSeparator(); //加分割线
    QAction *openPro = fileMenu->addAction("打开项目");

2. 创建工具栏

cpp 复制代码
    //工具栏
    QToolBar *toolbar = new QToolBar(this);
    //工具栏靠左侧
    addToolBar(Qt::LeftToolBarArea, toolbar);
    //添加快捷键
    toolbar->addAction(createPro);
    toolbar->addAction(openPro);

3. 创建状态栏

cpp 复制代码
    //状态栏创建
    QStatusBar *statusBar = new QStatusBar(this);
    setStatusBar(statusBar);
    //放标签
    QLabel *label = new QLabel("状态栏");
    //标签放入状态栏左侧
    statusBar->addWidget(label);
    QLabel *label2 = new QLabel("右侧状态栏");
    //标签放入状态栏右侧
    statusBar->addPermanentWidget(label2);

【运行结果】

4. 模态和非模态对话框创建

模态对话框:

cpp 复制代码
    //点击新建按钮弹出对话框
    connect(createPro, &QAction::triggered, this, [=]{
        //创建模态对话框:弹出该对话框后,若不关闭则不能对其它窗口进行操作。
        QDialog dialog1(this);
        dialog1.setWindowTitle("模态对话框");
        dialog1.resize(this->size()); //生成的对话框和窗口一样大
        dialog1.exec(); //阻塞
        qDebug() << "创建模态对话框" << Qt::endl;
    });

【运行结果】

非模态对话框

cpp 复制代码
    connect(createPro, &QAction::triggered, this, [=]{
        //创建非模态对话框:在不关闭该对话框的情况下可以对其它窗口进行操作。
        QDialog *dialog2 = new QDialog(this);
        dialog2->setWindowTitle("非模态对话框");
        dialog2->resize(this->size()); //生成的对话框和窗口一样大
        dialog2->show(); //show函数
        dialog2->setAttribute(Qt::WA_DeleteOnClose);
        qDebug() << "创建非模态对话框" << Qt::endl;
    });

【运行结果】

补充知识点:

英语部分后面补吧

相关推荐
晓梦林5 小时前
BUUCTF findKey-学习笔记
笔记·学习
0566465 小时前
agent学习Day15——SQLAlchemy 查询过滤、分页与历史列表接口
python·学习·fastapi
hPw0eKIqD6 小时前
C++ 模板参数推导问题小记(非推导上下文)
开发语言·c++
程序员爱德华6 小时前
Python与C++:异同点对比
c++·python
炎武丶航7 小时前
汽车功能测试学习(1):FCW前方碰撞预警
功能测试·学习·汽车
小灰灰搞电子9 小时前
Qt 实现魔法导航菜单源码分享
开发语言·qt
888CC++9 小时前
C语言与C++的区别:从面向过程到面向对象
java·c语言·c++
Quz10 小时前
QML SpinBox 基础与步长控制:可编辑输入与自定义步长
qt
Darkwanderor10 小时前
Linux系统编程实战项目:模拟实现shell
linux·c++
himobrinehacken11 小时前
揭秘Windows程序启动的神秘之旅
c++·安全