Qt from introduction to actual combat Chapter x_ xx_QMainWindow menu bar and toolbar (enumeration values in Qt start with "Qt::" and controls can be added with addwidget)

Keywords: Qt

QMainWindow is a class that provides users with main window programs, including a menu bar, multiple tool bars, multiple dock widgets, a status bar and a central widget. It is the basis of many applications, such as text editor, picture editor, etc.

This chapter also creates the interface in the form of code, so the settings of the following options are adopted:

The specific contents are as follows:

However, a small window will appear after running:

1. Create a menu bar. There can only be one menu bar
Get the menu bar pointer of the main window through the menubar() function of QMainWindow class

QMenuBar *	menuBar() const


Add the following code:

#include "mainwindow.h"
#include <QMenuBar>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    //Reset window size settings
    resize(600,400);

    //Menu bar creation
    QMenuBar* bar=menuBar();

    //Put the menu bar into the window. At this time, because there is no menu inside, it will not be displayed
    setMenuBar(bar);

    //Put menu in menu bar
    bar->addMenu("file");

}

MainWindow::~MainWindow()
{
}


The operation results are as follows:

(1) Create menu item

    //Create menu item
    fileMenu->addAction("new");


(2) Create separation lines

//Add split line
fileMenu->addSeparator();


Code for creating menu bar

    //Reset window size settings
    resize(600,400);

    //Menu bar only one menu bar can be created
    QMenuBar* bar=menuBar();

    //Put the menu bar into the window. At this time, because there is no menu inside, it will not be displayed
    setMenuBar(bar);

    //Put the menu in the menu bar, and the return value is QMenu
    QMenu* fileMenu= bar->addMenu("file");
    QMenu* editMenu= bar->addMenu("edit");

    //Create menu item
    fileMenu->addAction("new");

    //Add split line
    fileMenu->addSeparator();

    fileMenu->addAction("open");

2. Toolbar. There can be multiple toolbars

    //Toolbar, can have multiple
    QToolBar* toolBar=new QToolBar(this);
    addToolBar(toolBar);


(1) The toolbar is placed on the top by default. How to modify the placement?

You can see that it contains the placement parameters, but how to use it? Go to assistant to find:

After clicking in, you can see that the data types in the parameters have so many enumeration values. Select what you want to fill in. In Qt, enumeration values are written in the form of "Qt::".

If you add a toolbar to the left, change the command to:

addToolBar(Qt::LeftToolBarArea,toolBar);


(2) Set that only left and right docking is allowed

//Late settings only allow left and right docking
toolBar->setAllowedAreas(Qt::LeftToolBarArea | Qt::RightToolBarArea);


When you move to the top, you can see that it is in a floating state

(3) How to set it not to float?

//Set float
toolBar->setFloatable(false);

Set the mobile master switch

toolBar->setMovable(false);

(4) Add content to toolbar:

//Content can be set in the toolbar
QAction* debug=fileMenu->addAction("debug");
QAction* run=fileMenu->addAction("run");
toolBar->addAction(debug);
toolBar->addAction(run);


(5) Add a button to the toolbar
In Qt, all controls can be added using addwidget

QPushButton* btn=new QPushButton("aa",this);
toolBar->addWidget(btn);


3. Learning video address: QMainWindow menu bar and toolbar (enumeration values in Qt start with "Qt::" and controls can be added with addwidget)
4. Source code:

#include "mainwindow.h"
#include <QMenuBar>
#include <QToolBar>
#include <QPushButton>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    //Reset window size settings
    resize(600,400);

    //Menu bar only one menu bar can be created
    QMenuBar* bar=menuBar();

    //Put the menu bar into the window. At this time, because there is no menu inside, it will not be displayed
    setMenuBar(bar);

    //Put the menu in the menu bar, and the return value is QMenu
    QMenu* fileMenu= bar->addMenu("file");
    QMenu* editMenu= bar->addMenu("edit");

    //Create menu item
    fileMenu->addAction("new");

    //Add split line
    fileMenu->addSeparator();

    fileMenu->addAction("open");

    //Toolbar, which can have multiple toolbars. It is docked on the upper side by default
    QToolBar* toolBar=new QToolBar(this);
    //Set the toolbar to dock on the left
    addToolBar(Qt::LeftToolBarArea,toolBar);

    //Late settings only allow left and right docking
    toolBar->setAllowedAreas(Qt::LeftToolBarArea | Qt::RightToolBarArea);

    //Set float
    toolBar->setFloatable(false);

    //Set the mobile master switch
    toolBar->setMovable(false);

    //Content can be set in the toolbar
    QAction* debug=fileMenu->addAction("debug");
    QAction* run=fileMenu->addAction("run");

    toolBar->addAction(debug);

    toolBar->addSeparator();

    toolBar->addAction(run);

    //Add button to toolbar
    QPushButton* btn=new QPushButton("aa",this);
    toolBar->addWidget(btn);
}

MainWindow::~MainWindow()
{
}


Posted by run2web on Fri, 03 Dec 2021 14:22:53 -0800