Qt create menu bar, toolbar, status bar

QMainWindow allows you to create menu bars, toolbars, and status bars on it, and we create them on QMainWindow. Go directly to the code, there are detailed comments in the code.

//MainWindow.h文件
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QAction>
class MainWindow : public QMainWindow
{
    Q_OBJECT
    
public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
    
private:
};
#endif // MAINWINDOW_H
//MainWindow.cpp
#include "mainwindow.h"

//创建菜单栏,工具栏,状态栏应当包含的头文件
#include <QMenuBar>                         
#include <QMenu>
#include <QToolBar>
#include <QStatusBar>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    
    this->setWindowTitle("My Window");                           //设置窗口标题
    this->setMinimumSize(500,400);                                    //设置窗口最小尺寸。
   
    QMenuBar *menu_bar = new QMenuBar(this);             //创建一个菜单栏
    this->setMenuBar(menu_bar);                                         //设置为MainWindow的菜单栏
    QToolBar *tool_bar = new QToolBar(this);                    //创建一个工具栏
    this->addToolBar(tool_bar);                                            //添加工具栏到MainWindow
    QStatusBar *status_bar = new QStatusBar(this);            //创建一个状态栏
    this->setStatusBar(status_bar);                                        //设置为MainWindow的状态栏
    
    //创建菜单
    QMenu *file_menu = new QMenu("文件(&F)",menu_bar);    
    QMenu *edit_menu = new QMenu("编辑(&E)",menu_bar);
    QMenu *help_menu = new QMenu("帮助(&H)",menu_bar);
    
    //创建动作
    QAction *new_action = new QAction("新建(&N)");
    QAction *open_action = new QAction("打开(&O)");
    QAction *save_action = new QAction("保存(&S)");
    
    //添加状态栏提示
    new_action->setStatusTip("新建一个文件或项目");
    open_action->setStatusTip("打开一个文件或项目");
    save_action->setStatusTip("保存");
    
    //添加动作到新建菜单,QAction就会自动变成子菜单
    file_menu->addAction(new_action);
    file_menu->addAction(open_action);
    file_menu->addSeparator();                      //添加菜单分隔符
    file_menu->addAction(save_action);
    
    //给编辑菜单添加子菜单
    edit_menu->addAction("剪切(&T)");
    //给帮助菜单添加子菜单
    help_menu->addAction("关于(&A)");
    
    //把菜单添加到菜单栏
    menu_bar->addMenu(file_menu);
    menu_bar->addMenu(edit_menu);
    menu_bar->addMenu(help_menu);

    //把动作添加到工具栏,QAction就会自动变成工具
    tool_bar->addAction(new_action);
    tool_bar->addAction(open_action);
    tool_bar->addAction(save_action);   
}

MainWindow::~MainWindow()
{
}

//main.cpp
#include "mainwindow.h"

#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
}

The results are as follows: 

Maybe most people beginners see menu bar () to create a menu bar in the tutorial when they learn, and then they find that they are not the same as menuBar (). This is because you have not set the new menu bar as the menu bar of the current window. You need to use this-> setMenuBar (menu_bar); to set the new menu bar as the menu bar of the current window.

 Similarly, the tutorial you watched uses new to create the toolbar; but also uses statusBar () to create the status bar. Especially for a beginner, you may be thinking, who can remember whether to use new or function, this is too much trouble. So, you should all be new, and then add the toolbar to the current window; the status bar should also be set to the status bar of the current window. There is no need to remember.

We also saw the power of QAction, which represents an action, which can be added to the menu or to the toolbar. You can also set the prompt statement of the action in the status bar.

 

Published 242 original articles · Like 180 · Visits 160,000+

Guess you like

Origin blog.csdn.net/zy010101/article/details/105349217