做外贸免费的B2B网站沧州网站建设推广
目录
- 一、新建一个项目
- 二、效果展示
- 三、实现代码
- 1、mainwindow.h:头文件
- 2、mainwindow.cpp:源文件
- 3、main.cpp:主程序
一、新建一个项目
具体步骤可查看Qt 5.14.2入门(一)写个Hello Qt!程序
二、效果展示
1、整个窗口包括菜单栏和工具栏(具体内容可自己修改)
2、菜单栏包括点击下拉功能,包含新建、打开和退出。(可根据自己的需要添加功能)
3、每个功能只给出了动作响应(信息框提示)
三、实现代码
1、mainwindow.h:头文件
/*** @file mainwindow.h* @brief 主窗口头文件,包含 MainWindow 类的声明** 该文件定义了应用程序的主窗口类,包含菜单栏、工具栏和状态栏的声明*/#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow> // 主窗口基类
#include <QMenuBar> // 菜单栏
#include <QMenu> // 菜单
#include <QAction> // 菜单项/工具栏按钮动作
#include <QToolBar> // 工具栏
#include <QStatusBar> // 状态栏
#include <QLabel> // 标签(用于中央部件)
#include <QMessageBox> // 消息框/*** @class MainWindow* @brief 应用程序主窗口类** 继承自 QMainWindow,提供标准的主窗口功能,* 包括菜单栏、工具栏、状态栏和中央部件区域*/
class MainWindow : public QMainWindow {Q_OBJECT // 必须的宏,启用Qt元对象系统(信号槽等)public:/*** @brief 构造函数* @param parent 父窗口指针,默认为nullptr*/explicit MainWindow(QWidget *parent = nullptr);private:/*** @brief 创建菜单栏及其内容*/void createMenuBar();/*** @brief 创建工具栏及其内容*/void createToolBar();private slots:// 以下是各个菜单项/工具按钮的槽函数void onNew(); ///< 新建文件动作响应void onOpen(); ///< 打开文件动作响应void onSave(); ///< 保存文件动作响应void onCopy(); ///< 复制动作响应void onPaste(); ///< 粘贴动作响应void onAbout(); ///< 关于对话框动作响应
};#endif // MAINWINDOW_H
2、mainwindow.cpp:源文件
/*** @file mainwindow.cpp* @brief MainWindow 类的实现文件** 实现主窗口的各项功能,包括菜单栏、工具栏的创建和事件处理*/#include "mainwindow.h"/*** @brief MainWindow 构造函数* @param parent 父窗口指针** 初始化主窗口,设置标题、大小,创建界面元素*/
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
{// 设置窗口标题和初始大小setWindowTitle("Qt5 菜单栏和工具栏 Demo");resize(800, 600);createMenuBar(); // 初始化菜单栏createToolBar(); // 初始化工具栏// 初始化状态栏并显示默认消息QStatusBar *statusBar = this->statusBar();statusBar->showMessage("就绪");// 设置中央部件为一个居中对齐的标签QLabel *centralLabel = new QLabel("这是中央区域", this);centralLabel->setAlignment(Qt::AlignCenter);setCentralWidget(centralLabel);
}/*** @brief 创建菜单栏及其内容** 创建文件、编辑、帮助三个主菜单及其子菜单项* 每个菜单项都设置了快捷键和状态栏提示*/
void MainWindow::createMenuBar()
{// ================== 文件菜单 ==================QMenu *fileMenu = menuBar()->addMenu("文件(&F)"); // &F表示Alt+F快捷键// 新建动作QAction *newAction = new QAction("新建(&N)", this);newAction->setShortcut(QKeySequence::New); // 设置快捷键Ctrl+NnewAction->setStatusTip("创建一个新文件"); // 状态栏提示connect(newAction, &QAction::triggered, this, &MainWindow::onNew);fileMenu->addAction(newAction);// 打开动作QAction *openAction = new QAction("打开(&O)...", this);openAction->setShortcut(QKeySequence::Open);openAction->setStatusTip("打开一个文件");connect(openAction, &QAction::triggered, this, &MainWindow::onOpen);fileMenu->addAction(openAction);fileMenu->addSeparator(); // 添加分隔线// 退出动作QAction *exitAction = new QAction("退出(&X)", this);exitAction->setShortcut(QKeySequence::Quit);exitAction->setStatusTip("退出应用程序");connect(exitAction, &QAction::triggered, this, &QMainWindow::close);fileMenu->addAction(exitAction);// ================== 编辑菜单 ==================QMenu *editMenu = menuBar()->addMenu("编辑(&E)");// 复制动作QAction *copyAction = new QAction("复制(&C)", this);copyAction->setShortcut(QKeySequence::Copy);copyAction->setStatusTip("复制选中内容");connect(copyAction, &QAction::triggered, this, &MainWindow::onCopy);editMenu->addAction(copyAction);// 粘贴动作QAction *pasteAction = new QAction("粘贴(&P)", this);pasteAction->setShortcut(QKeySequence::Paste);pasteAction->setStatusTip("粘贴内容");connect(pasteAction, &QAction::triggered, this, &MainWindow::onPaste);editMenu->addAction(pasteAction);// ================== 帮助菜单 ==================QMenu *helpMenu = menuBar()->addMenu("帮助(&H)");// 关于动作QAction *aboutAction = new QAction("关于(&A)...", this);aboutAction->setStatusTip("显示关于对话框");connect(aboutAction, &QAction::triggered, this, &MainWindow::onAbout);helpMenu->addAction(aboutAction);
}/*** @brief 创建工具栏及其内容** 创建文件和编辑两个工具栏,包含带图标的工具按钮* 工具按钮与菜单项共享相同的槽函数*/
void MainWindow::createToolBar() {// ================== 文件工具栏 ==================QToolBar *fileToolBar = addToolBar("文件"); // 创建工具栏并命名// 新建按钮(使用系统主题图标)QAction *newToolAction = new QAction(QIcon::fromTheme("document-new"), "新建", this);newToolAction->setShortcut(QKeySequence::New);connect(newToolAction, &QAction::triggered, this, &MainWindow::onNew);fileToolBar->addAction(newToolAction);// 打开按钮QAction *openToolAction = new QAction(QIcon::fromTheme("document-open"), "打开", this);openToolAction->setShortcut(QKeySequence::Open);connect(openToolAction, &QAction::triggered, this, &MainWindow::onOpen);fileToolBar->addAction(openToolAction);fileToolBar->addSeparator(); // 工具栏分隔线// 保存按钮(菜单中没有对应的项,是工具栏特有功能)QAction *saveToolAction = new QAction(QIcon::fromTheme("document-save"), "保存", this);saveToolAction->setShortcut(QKeySequence::Save);connect(saveToolAction, &QAction::triggered, this, &MainWindow::onSave);fileToolBar->addAction(saveToolAction);// ================== 编辑工具栏 ==================QToolBar *editToolBar = addToolBar("编辑");// 复制按钮QAction *copyToolAction = new QAction(QIcon::fromTheme("edit-copy"), "复制", this);copyToolAction->setShortcut(QKeySequence::Copy);connect(copyToolAction, &QAction::triggered, this, &MainWindow::onCopy);editToolBar->addAction(copyToolAction);// 粘贴按钮QAction *pasteToolAction = new QAction(QIcon::fromTheme("edit-paste"), "粘贴", this);pasteToolAction->setShortcut(QKeySequence::Paste);connect(pasteToolAction, &QAction::triggered, this, &MainWindow::onPaste);editToolBar->addAction(pasteToolAction);
}// ================== 以下是各个动作的槽函数实现 ==================/*** @brief 新建文件动作响应* 显示一个信息框提示新建操作被触发*/
void MainWindow::onNew() {QMessageBox::information(this, "新建", "新建文件操作被触发");
}/*** @brief 打开文件动作响应* 显示一个信息框提示打开操作被触发*/
void MainWindow::onOpen() {QMessageBox::information(this, "打开", "打开文件操作被触发");
}/*** @brief 保存文件动作响应* 显示一个信息框提示保存操作被触发*/
void MainWindow::onSave() {QMessageBox::information(this, "保存", "保存文件操作被触发");
}/*** @brief 复制动作响应* 显示一个信息框提示复制操作被触发*/
void MainWindow::onCopy() {QMessageBox::information(this, "复制", "复制操作被触发");
}/*** @brief 粘贴动作响应* 显示一个信息框提示粘贴操作被触发*/
void MainWindow::onPaste() {QMessageBox::information(this, "粘贴", "粘贴操作被触发");
}/*** @brief 关于对话框动作响应* 显示应用程序的关于信息*/
void MainWindow::onAbout() {QMessageBox::about(this, "关于","Qt5 菜单栏和工具栏 Demo\n""版本 1.0\n""演示Qt5主窗口的基本功能");
}
3、main.cpp:主程序
/*** @file main.cpp* @brief 应用程序入口文件** 创建QApplication实例和主窗口,启动事件循环*/#include "mainwindow.h" // 主窗口头文件
#include <QApplication> // Qt应用程序类/*** @brief 应用程序主入口* @param argc 命令行参数个数* @param argv 命令行参数数组* @return 应用程序退出代码** 创建并显示主窗口,启动Qt事件循环*/
int main(int argc, char *argv[]) {QApplication app(argc, argv); // 创建应用程序对象,管理整个应用程序MainWindow mainWindow; // 创建主窗口实例mainWindow.show(); // 显示主窗口return app.exec(); // 进入主事件循环,等待用户操作
}