QMainWindow
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.
menu bar
A main window has at most one menu bar. Located at the top of the main window, below the title bar of the main window.
//Create a menu bar and obtain the menu bar pointer of the main window through the menubar() function of QMainWindow class QMenuBar * menuBar() const //Create a menu and call the member function addMenu of QMenu to add a menu QAction* addMenu(QMenu * menu) QMenu* addMenu(const QString & title) QMenu* addMenu(const QIcon & icon, const QString & title) //Create a menu item and call the member function addAction of QMenu to add the menu item QAction* activeAction() const QAction* addAction(const QString & text) QAction* addAction(const QIcon & icon, const QString & text) QAction* addAction(const QString & text, const QObject * receiver, const char * member, const QKeySequence & shortcut = 0) QAction* addAction(const QIcon & icon, const QString & text, const QObject * receiver, const char * member, const QKeySequence & shortcut = 0)
Qt does not have a special menu item class, but uses a QAction class to abstract out public actions. When we add the QAction object to the menu, it will be displayed as a menu item. When we add it to the toolbar, it will be displayed as a tool button. The user can activate this action by clicking the menu item, clicking the toolbar button and clicking the shortcut key.
toolbar
There can be multiple toolbars on the toolbar of the main window. Usually, one menu corresponds to one toolbar. Toolbars can also be divided as needed.
Directly call the addToolBar() function of QMainWindow class to obtain the toolbar object of the main window. This function needs to be called every time a toolbar is added.
Insert an action that belongs to the toolbar, that is, add an action on the toolbar.
Add through the addAction function of QToolBar class.
The toolbar is a movable window whose docking area is determined by the allowAreas of QToolBar, including:
Qt::LeftToolBarArea Dock on the left Qt::RightToolBarArea Dock on the right Qt::TopToolBarArea Dock on top Qt::BottomToolBarArea Dock at bottom Qt::AllToolBarAreas The above four positions can be docked use setAllowedAreas()Function to specify the docking area: setAllowedAreas(Qt::LeftToolBarArea | Qt::RightToolBarArea) use setMoveable()Function to set the mobility of the toolbar: setMoveable(false)//The toolbar cannot be moved and can only be docked at the initialized position
status bar
Derived from QWidget class, the use method is similar to QWidget. Common member functions of QStatusBar class:
There can only be at most one status bar
//Add widget void addWidget(QWidget * widget, int stretch = 0) //Insert widget int insertWidget(int index, QWidget * widget, int stretch = 0) //Delete widget void removeWidget(QWidget * widget)
Riveted parts
The riveted part QDockWidget, also known as a floating window, can have multiple.
QDockWidget * dock = new QDockWidget("title",this); addDockWidget(Qt::LeftDockWidgetArea,dock); dock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea | Qt::TopDockWidgetArea); Set area range
Core component (central component)
#In addition to the above components, the components displayed in the center can be used as core components. For example, a notepad file can use QTextEdit as core components
QTextEdit * edit = new QTextEdit(this); setCentralWidget(edit);
Realize the relevant settings of menu bar, status bar and toolbar
#include "mainwindow.h" #include "ui_mainwindow.h" #Include < qmenubar > / / contains the header file of the menu bar #Include < qtoolbar > / / contains the header file of the toolbar #include<QDebug> #include<QPushButton> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { //Reset window size resize(500,400); //There can be at most one menu bar //The created menu bar will not be placed in the window by default QMenuBar * bar=menuBar(); //Place the menu bar in the window setMenuBar(bar); //create menu QMenu * fileMenu = bar->addMenu("file"); QMenu * editMenu = bar->addMenu("edit"); //Create a drop-down menu for the menu QAction * newAction = fileMenu->addAction(("newly build")); QAction * openAciton=fileMenu->addAction(("open")); //Add a separator in the drop-down menu fileMenu->addSeparator(); fileMenu->addAction(("delete")); fileMenu->addAction(("sign out")); //********************************************************** //Create toolbars toolbars can have multiple QToolBar * toolBar=new QToolBar(this); //Place the toolbar in the window addToolBar(Qt::RightToolBarArea,toolBar);//Place the toolbar on the left //Later settings only allow the toolbar to dock left and right toolBar->setAllowedAreas( Qt::LeftToolBarArea | Qt::RightToolBarArea ); //Set float toolBar->setFloatable(false); //Set the mobile master switch toolBar->setMovable(false); //Set content in toolbar toolBar->addAction(newAction);//Add new function toolBar->addSeparator();//Add split line toolBar->addAction(openAciton);//Add open function //Add control to toolbar QPushButton * btn=new QPushButton("aa",this); toolBar->addWidget(btn); //**************************************************** //The status bar has at most one m, which is not in the window by default QStatusBar * stBar=statusBar(); //Place the status bar in the window setStatusBar(stBar); //Drop label control QLabel * label=new QLabel("Prompt information",this); stBar->addWidget(label); QLabel * label2=new QLabel("Prompt message on the right",this); stBar->addPermanentWidget(label2); //There can be multiple riveted parts (floating windows) QDockWidget*dockWidget=new QDockWidget("float",this); addDockWidget(Qt::BottomDockWidgetArea,dockWidget); //Setting the late docking area allows only up and down dockWidget->setAllowedAreas(Qt::TopDockWidgetArea | Qt::BottomDockWidgetArea); //There can only be one setup center assembly QTextEdit * edit=new QTextEdit(this); setCentralWidget(edit); //ui->setupUi(this); } MainWindow::~MainWindow() { delete ui; }
The set interface is as follows: