用代码来实现菜单栏:
头文件:mainwindow.h
- #ifndef MAINWINDOW_H
- #define MAINWINDOW_H
-
- #include
-
- QT_BEGIN_NAMESPACE
- namespace Ui { class MainWindow; }
- QT_END_NAMESPACE
-
- class MainWindow : public QMainWindow
- {
- Q_OBJECT
-
- public:
- MainWindow(QWidget *parent = nullptr);
- ~MainWindow();
-
- void craeteMenuBar(); //创建菜单栏
-
- void craeteToolBar(); //创建工具栏
-
- void craeteStatusBar(); //创建状态栏
-
- public slots:
- void clicknewMenuAction(); //选择新建文件触发的槽函数
- void clickopenMenuAction(); //选择打开文件触发的槽函数
- void clickcutToolbarAction(); //选择剪切工具项触发的槽函数
- void clickitalicToolbarAction();//选择斜体工具项触发的槽函数
-
- private:
- QMenu* m_menu;
- QAction* m_newMenu;
- QAction* m_openMenu;
-
- QToolBar* m_Toolbar;
- QAction* m_Action1;
- QAction* m_Action2;
- QAction* m_Action3;
- QAction* m_Action4;
-
-
- private:
- Ui::MainWindow *ui;
- };
-
- #endif // MAINWINDOW_H
-
本文福利,莬费领取Qt开发学习资料包、技术视频,内容包括(C++语言基础,Qt编程入门,QT信号与槽机制,QT界面开发-图像绘制,QT网络,QT数据库编程,QT项目实战,QT嵌入式开发,Quick模块等等)↓↓↓↓↓↓见下面↓↓文章底部点击莬费领取↓↓
源文件:mainwindow.cpp
- #include "mainwindow.h"
- #include "ui_mainwindow.h"
-
- #include <QMenu>
- #include <QToolBar>
- #include <QAction>
- #include <QMessageBox>
- #include <QDebug>
- #include <QLabel>
-
- MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
- {
- ui->setupUi(this);
-
- craeteMenuBar();
-
- //craeteToolBar();
-
- //craeteStatusBar();
- }
-
- MainWindow::~MainWindow()
- {
- delete ui;
- }
-
- //创建菜单栏
- void MainWindow::craeteMenuBar()
- {
- //新增主菜单
- m_menu = menuBar()->addMenu(tr("文件"));
-
- //创建子菜单
- m_newMenu = new QAction(QIcon("://images/new.png"),tr("新建文件"),this);
- m_openMenu = new QAction(QIcon("://images/open.png"),tr("打开文件"),this);
-
- //设置打开文件动作的快捷键
- //newMenuAction->setShortcut("Ctrl + N");
- //openMenuAction->setShortcut("Ctrl + O");
-
- //设置打开文件动作的提示信息
- m_newMenu->setStatusTip(tr("这是新建文件的提示信息"));
- m_openMenu->setStatusTip(tr("这是打开文件的提示信息"));
-
- //把子菜单和父菜单关联
- m_menu->addAction(m_newMenu);
- m_menu->addAction(m_openMenu);
-
- //关联新建文件动作的信号和槽
- connect(m_newMenu,SIGNAL(triggered()),this,SLOT(clicknewMenuAction()));
- //关联打开文件动作的信号和槽
- connect(m_openMenu,SIGNAL(triggered()),this,SLOT(clickopenMenuAction()));
- }
-
- //选择新建文件触发的槽函数
- void MainWindow::clicknewMenuAction()
- {
- QMessageBox MyBox(QMessageBox::Question,"Title","newMenu的槽函数已触发!",QMessageBox::Yes | QMessageBox::No);
- MyBox.exec();
- }
-
- //选择打开文件触发的槽函数
- void MainWindow::clickopenMenuAction()
- {
- QMessageBox MyBox(QMessageBox::Question,"Title","openMenu的槽函数已触发!",QMessageBox::Yes | QMessageBox::No);
- MyBox.exec();
- }
添加代码之后的运行效果如下:

点击新建文件触发槽函数:

用代码来实现工具栏:
头文件和菜单栏的一样就行
源文件:mainwindow.cpp
- #include "mainwindow.h"
- #include "ui_mainwindow.h"
-
- #include <QMenu>
- #include <QToolBar>
- #include <QAction>
- #include <QMessageBox>
- #include <QDebug>
- #include <QLabel>
-
- MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
- {
- ui->setupUi(this);
-
- //craeteMenuBar();
-
- craeteToolBar();
-
- //craeteStatusBar();
- }
-
- //创建工具栏
- void MainWindow::craeteToolBar()
- {
- //方法1:构建工具栏
- //m_Toolbar = addToolBar("file");
-
- //方法2:构建工具栏
- m_Toolbar = new QToolBar(this);
- this->addToolBar(m_Toolbar);
-
- //构建工具栏选项
- m_Action1 = new QAction(QIcon("://images/cut.png"),tr("剪切"),this);
- m_Action2 = new QAction(QIcon("://images/italic.png"),tr("斜体"),this);
- m_Action3 = new QAction(tr("保存"),this);
- m_Action4 = new QAction(tr("退出"),this);
-
- //将工具栏选项添加到工具栏
- m_Toolbar->addAction(m_Action1);
- m_Toolbar->addAction(m_Action2);
- m_Toolbar->addAction(m_Action3);
- m_Toolbar->addAction(m_Action4);
-
- //动作连接槽函数
- connect(m_Action1, SIGNAL(triggered()), this, SLOT(clickcutToolbarAction()));
- connect(m_Action2, SIGNAL(triggered()), this, SLOT(clickitalicToolbarAction()));
- }
-
- //选择剪切工具项触发的槽函数
- void MainWindow::clickcutToolbarAction()
- {
- QMessageBox MyBox(QMessageBox::Question,"Title","cutToolBar的槽函数已触发!",QMessageBox::Yes | QMessageBox::No);
- MyBox.exec();
- }
-
- //选择斜体工具项触发的槽函数
- void MainWindow::clickitalicToolbarAction()
- {
- QMessageBox MyBox(QMessageBox::Question,"Title","italicToolBar的槽函数已触发!",QMessageBox::Yes | QMessageBox::No);
- MyBox.exec();
- }
-
添加代码之后的运行效果如下:

点击剪切触发槽函数:

用代码来实现状态栏:
头文件和菜单栏的一样就行
源文件:mainwindow.cpp
- #include "mainwindow.h"
- #include "ui_mainwindow.h"
-
- #include <QMenu>
- #include <QToolBar>
- #include <QAction>
- #include <QMessageBox>
- #include <QDebug>
- #include <QLabel>
-
- MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
- {
- ui->setupUi(this);
-
- //craeteMenuBar();
-
- //craeteToolBar();
-
- craeteStatusBar();
- }
-
- //创建状态栏
- void MainWindow::craeteStatusBar()
- {
- QLabel* label1 = new QLabel(tr("Label1"),this);
- QLabel* label2 = new QLabel(tr("Label2"),this);
- QLabel* label3 = new QLabel(tr("Label3"),this);
- statusBar()->addWidget(label1,1);
- statusBar()->addWidget(label2,1);
- statusBar()->addWidget(label3,1);
- }
添加代码之后的运行效果如下:

本文福利,莬费领取Qt开发学习资料包、技术视频,内容包括(C++语言基础,Qt编程入门,QT信号与槽机制,QT界面开发-图像绘制,QT网络,QT数据库编程,QT项目实战,QT嵌入式开发,Quick模块等等)↓↓↓↓↓↓见下面↓↓文章底部点击莬费领取↓↓