The menu bar

The menu bar

To use the menu bar control, import and header files. Create a QMainWindow statement with a menu bar as follows:

#include "MainWindow.h"
#include "ui_MainWindow.h"
#include <QMenuBar>
#include <QMenu>
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    QMenuBar *mbar=menuBar(a);// Note that the new keyword is not needed here
    setMenuBar(mbar);    // The menu bar will appear if you write it or not, but you'd better write it anyway
 
}
 
MainWindow::~MainWindow()
{
    delete ui;
}
Copy the code

The implementation effect is as follows:

Now that we have the menu bar to load the menu, we can add menu labels to it.

P.S: setMenuBar() will display the menu bar if it is not written, but it is best to write it to avoid unnecessary errors.

Menu labelling

To add a menu, call the addMenu() method of the QMenuBar object and receive it with a QMenu object pointer.

#include "MainWindow.h"
#include "ui_MainWindow.h"
#include <QMenuBar>
#include <QMenu>
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    / / the menu bar
    QMenuBar *mbar=menuBar(a);// Add menu
    QMenu *pFile=mbar->addMenu("File");
}
 
MainWindow::~MainWindow()
{
    delete ui;
}
Copy the code

A menu TAB called file now appears in the window.

A menu item

Under normal circumstances, we click the menu TAB should pop up some menu items, how to achieve?

Adding a menu item requires calling the addAction method of the QMenu object and receiving it with a QAction object pointer.

#include "MainWindow.h"
#include "ui_MainWindow.h"
#include <QMenuBar>
#include <QMenu>
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    / / the menu bar
    QMenuBar *mbar=menuBar(a);// Add menu
    QMenu *pFile=mbar->addMenu("File");
    // Add a menu item
    QAction *pCreate=pFile->addAction("New");
}
 
MainWindow::~MainWindow()
{
    delete ui;
}
Copy the code

Effect:

 

Now we have a menu label with a new menu item.

The divider

The menu items of each menu label are often divided by dividing lines according to different functions. How to achieve dividing lines?

To implement the splitter, call the addSeperator method with the QMenu object.

#include "MainWindow.h"
#include "ui_MainWindow.h"
#include <QMenuBar>
#include <QMenu>
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    / / the menu bar
    QMenuBar *mbar=menuBar(a);// Add menu
    QMenu *pFile=mbar->addMenu("File");
    // Add a menu item
    QAction *pCreate=pFile->addAction("New");
    QAction *pOpen=pFile->addAction("Open");
    QAction *pSave=pFile->addAction("Save");
    // Add a divider
    pFile->addSeparator(a); QAction *pExit=pFile->addAction("Quit");
    connect(pCreate,&QAction::triggered,[=](bool isTrigger){
        qDebug() < <"New is pressed";
        qDebug()<<isTrigger;
    });
}
 
MainWindow::~MainWindow()
{
    delete ui;
}
Copy the code

Effect:

 

Where the separator appears depends on where the addSeparator() statement appears in the code. If you place it after the statement that creates the Open menu item, the split line appears after the Open menu item.

The toolbar

The toolbar

Toolbars are shortcuts to menu items. Just like in Word, you can save a document from the menu bar or click the “Save” icon in the toolbar.

To use a menu bar control, you need to import a header file. The syntax for creating a toolbar to load toolbars is as follows:

#include "MainWindow.h"
#include "ui_MainWindow.h"
#include <QMenuBar>
#include <QMenu>
#include <QDebug>
#include <QToolBar>
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    / / the menu bar
    QMenuBar *mbar=menuBar(a);// Add menu
    QMenu *pFile=mbar->addMenu("File");
    // Add a menu item
    QAction *pCreate=pFile->addAction("New");
    QAction *pOpen=pFile->addAction("Open");
    QAction *pSave=pFile->addAction("Save");
    // Add a divider
    pFile->addSeparator(a); QAction *pExit=pFile->addAction("Quit");
    connect(pCreate,&QAction::triggered,[=](bool isTrigger){
        qDebug() < <"New is pressed";
        qDebug()<<isTrigger;
    });
    // Toolbar: shortcut to the menu bar
    QToolBar *mToolBar=addToolBar("tool");    / / please please please please please please
}
 
MainWindow::~MainWindow()
{
    delete ui;
}
Copy the code

Effect:

Tool item

Toolbar items are shortcuts to some menu items in the menu bar, so simply pass a pointer of type QAction to the menu item as a parameter to addAction() in the toolbar QToolBar object. The implementation code is as follows:

#include "MainWindow.h"
#include "ui_MainWindow.h"
#include <QMenuBar>
#include <QMenu>
#include <QDebug>
#include <QToolBar>
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    / / the menu bar
    QMenuBar *mbar=menuBar(a);// Add menu
    QMenu *pFile=mbar->addMenu("File");
    // Add a menu item
    QAction *pCreate=pFile->addAction("New");
    QAction *pOpen=pFile->addAction("Open");
    QAction *pSave=pFile->addAction("Save");
    // Add a divider
    pFile->addSeparator(a); QAction *pExit=pFile->addAction("Quit");
    connect(pCreate,&QAction::triggered,[=](bool isTrigger){
        qDebug() < <"New is pressed";
        qDebug()<<isTrigger;
    });
    // Toolbar: shortcut to the menu bar
    QToolBar *mToolBar=addToolBar("tool");
    // Add shortcut keys to toolbar
    mToolBar->addAction(pCreate);    / / please please please please please please please
}
 
MainWindow::~MainWindow()
{
    delete ui;
}
Copy the code

Effect:

As you can see, clicking the “New” option in the toolbar has the same effect as clicking the “New” option in the menu bar.

If we want the toolbar shortcut style to be less monotonous, we can also use buttons (and images later) to achieve this.

#include "MainWindow.h"
#include "ui_MainWindow.h"
#include <QMenuBar>
#include <QMenu>
#include <QDebug>
#include <QToolBar>
#include <QPushButton>    // To use the button, you must import this header file
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    / / the menu bar
    QMenuBar *mbar=menuBar(a);// Add menu
    QMenu *pFile=mbar->addMenu("File");
    // Add a menu item
    QAction *pCreate=pFile->addAction("New");
    QAction *pOpen=pFile->addAction("Open");
    QAction *pSave=pFile->addAction("Save");
    // Add a divider
    pFile->addSeparator(a); QAction *pExit=pFile->addAction("Quit");
    connect(pCreate,&QAction::triggered,[=](bool isTrigger){
        qDebug() < <"New is pressed";
        qDebug()<<isTrigger;
    });
    // Toolbar: shortcut to the menu bar
    QToolBar *mToolBar=addToolBar("tool");
    // Add shortcut keys to toolbar
    mToolBar->addAction(pCreate);
    // Add a widget
    QPushButton *b=new QPushButton(this);
    mToolBar->addWidget(b);
    b->setText("Button implementation shortcut");
}
 
MainWindow::~MainWindow()
{
    delete ui;
}
Copy the code

Effect:

 

The status bar

The status bar usually appears in the bottom line of the window, such as the status bar in Word, showing the total number of pages, the current page is the page.

To use the status bar, you must import the header file first. The status bar usually needs to display some text content, so you must import the header file. The syntax of the status bar is as follows:

#include "MainWindow.h"
#include "ui_MainWindow.h"
#include <QMenuBar>
#include <QMenu>
#include <QDebug>
#include <QToolBar>
#include <QPushButton>
#include <QStatusBar>    // Implement the header file that the status bar must import
#include <QLabel>    // Implement the header file that the status bar must import
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    // This part of the code is skipped
    / / the status bar
    QStatusBar *pStatusBar=statusBar(a);// Create status bar
    QLabel *pLabel=new QLabel(this);    // Specifies the parent element, the current window, for the label object
    pLabel->setText("Status bar contents");    // Set the text content
    pStatusBar->addWidget(pLabel);    // Add the label object as a control to the status bar object
}
 
MainWindow::~MainWindow()
{
    delete ui;
}
Copy the code

Effect:

You can see that a status bar is generated in the lower left corner of the window, and the label is added to the status bar because we call the addWidget method on the status bar object and pass the label object as a parameter.

The P.S: addWidget() method is used to add controls to the layout. And it’s left-aligned.

You can also add one more item to the status bar using another shorthand for addWidget.

// mainwindow.cpp A dynamically applied QLabel pointer is used as an argument to the status bar object's addWidget method. // The label content and its parent pStatusBar->addWidget(new) can be specified directly in the constructor of QLabel() QLabel("Content",this));Copy the code

Effect:

 

In the status bar of Word, some contents appear on the right side of the status bar, such as “zoom”, etc. How to achieve the effect of right-aligned labels?

This can be done using the addPermanentWidget method.

//MainWindow.cpp
pStatusBar->addPermanentWidget(new QLabel("Right".this));
Copy the code

Effect:

 

If you use addPermanentWidgets to add multiple tags, do you add them from right to left? We can experiment with the following code.

//MainWindow.cpp
pStatusBar->addPermanentWidget(new QLabel("Right".this));
pStatusBar->addPermanentWidget(new QLabel("afterRight".this));     //afterRight tags are added afterRight tags
Copy the code

Effect:

Conclusion: Both addWidgets () and AddPermanentWidgets () add tags from left to right, the difference being that the former is immediately to the left of the status bar, and the latter is immediately to the right.

Central widget

The setCentralWidget can add a central widget to the object from which it is called.

For example, use the following code to add a label to the main window as the center widget of the main window.

#include "MainWindow.h"
#include "ui_MainWindow.h"
#include <QMenuBar>
#include <QMenu>
#include <QDebug>
#include <QToolBar>
#include <QPushButton>
#include <QStatusBar>
#include <QLabel>
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    // This part of the code is skipped
    // The central widget
    setCentralWidget(new QLabel("center"));
 
}
 
MainWindow::~MainWindow()
{
    delete ui;
}
Copy the code

Effect:

 

In this way, the “Center” TAB is added to the main window as the center widget, but this obviously doesn’t make sense. We can add a text editor widget to the main window.

To use the text editing widget, you need to import a header file. The implementation code is as follows:

#include "MainWindow.h"
#include "ui_MainWindow.h"
#include <QMenuBar>
#include <QMenu>
#include <QDebug>
#include <QToolBar>
#include <QPushButton>
#include <QStatusBar>
#include <QLabel>
#include <QTextEdit>    // Implement the header file that the text edit box must import
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    // This part of the code is skipped
    // The central widget
    setCentralWidget(new QLabel("center"));
    QTextEdit *pTextEdit=new QTextEdit(this);
    setCentralWidget(pTextEdit);    //setCentralWidget is a MainWindow method, omitting this
}
 
MainWindow::~MainWindow()
{
    delete ui;
}
Copy the code

Effect:

P.S: The center widget added to the main window via the setCentralWidget method fills the entire main window by default, so if there are multiple center widgets added to the main window, the first ones are overwritten by the later ones, and only the last center widget is displayed.

Floating window

A floating window is a draggable widget in the main window. For example, the console window in Code Blocks is usually embedded in the main window, or you can drag it out separately.

To implement a floating window, you must first introduce header files.

The main window calls the addDockWidget() method, which has two overloaded functions that can be toggled by pressing ↑ and ↓ on the keyboard. Point to the function name and press F1 to see details about the function in the help documentation.

Here we choose an overloaded function that takes only two arguments, the first specifying the floating range and the second specifying the floating window.

How to specify the floating range? Click on Qt::DockWidgetArea in the help documentation and you will see the following:

This is a few constant variables, can choose the floating mode has left float, right float, float, float and so on.

#include "MainWindow.h"
#include "ui_MainWindow.h"
#include <QMenuBar>
#include <QMenu>
#include <QDebug>
#include <QToolBar>
#include <QPushButton>
#include <QStatusBar>
#include <QLabel>
#include <QTextEdit>
#include <QDockWidget>    // The header file that must be imported to use the floating window
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    // This part of the code is skipped
    // Float window
    QDockWidget *pDock=new QDockWidget(this);
    addDockWidget(Qt::LeftDockWidgetArea,pDock);    // Set the floating mode to left and the floating window to pDock
}
 
MainWindow::~MainWindow()
{
    delete ui;
}
Copy the code

Effect:

As you can see, a floating window appears to the left of the main window and pushes the center widget to the right. Drag the floating window freely, you can also manually float right, float up and float down.

Note: The object that calls this method is the main window!

Similarly, you can add widgets to a floating window, such as a “floating Window control” TAB.

#include "MainWindow.h"
#include "ui_MainWindow.h"
#include <QMenuBar>
#include <QMenu>
#include <QDebug>
#include <QToolBar>
#include <QPushButton>
#include <QStatusBar>
#include <QLabel>
#include <QTextEdit>
#include <QDockWidget>
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    // This part of the code is omitted
    // Float window
    QDockWidget *pDock=new QDockWidget(this);
    addDockWidget(Qt::LeftDockWidgetArea,pDock);
    // Add controls to a floating window
    pDock->setWidget(new QLabel("Floating Window control"));
}
 
MainWindow::~MainWindow()
{
    delete ui;
}
Copy the code

Effect:

P.S: ORIGINALLY, I tried to add the menu item pCreate to the floating window. After executing the program, an error message was reported:

E:\Demo\QtDemo\BilibiliTest\ 05_qmainWindow. CPP :56: error: C2664: “void QDockWidget::setWidget(QWidget *)” : ** Cannot convert parameter 1 from “QAction” to “QWidget”