Table of contents
1. Dock windows
Dock window refers to some windows that can be docked in OMainWindow or floated as independent windows. OMainWindow provides 4 docking window areas respectively at the top, bottom, left and right of the central widget.
Applications such as Microsoft Visual Studio and 0t Linguist make extensive use of docked windows to provide a very flexible user interface approach.
Each docked window has its own title bar, which is also displayed when it is docked. By dragging the title bar, the user can move the docking window from one docking area to another docking area. By dragging this docking window to the outside of other docking areas, you can separate the docking window from a docking area and make it an independent window.
Free-floating docked windows are always displayed on top of their main window. It can be closed by clicking the "Close" button on the title bar of the widget QDockWidget
. QDockWidget::setFeatures()
All of these features, and any combination of them, can be disabled by calling
The four corners shown with dashed lines can belong to either of the two adjacent docking areas. For example, suppose we need the upper left corner to belong to the left docking area, just call it QMainWinidow::setCorner(Qt::TopLeftCorner, Qt::LeftDockWid-getArea)
.
2. Try to write a docking window
The following piece of code is very simple, you only need to create a QT project, put this section into the constructor of your main window to run the program and it can be used. Of course remember to add QDockWidget
the header file.
QDockWidget* shapesDockWidget = new QDockWidget(tr("Shapes"));
shapesDockWidget->setObjectName("shapesDockWidgt");
QTreeWidget *treeWidget = new QTreeWidget(this);
shapesDockWidget->setWidget(treeWidget);
shapesDockWidget->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
addDockWidget(Qt::RightDockWidgetArea, shapesDockWidget);
setAllowedAreas()
The call specification accepts a docked window by restricting the docking area. In the code given here, the dock window is only allowed to be dragged to the left and right docking areas, both of which have enough vertical space to display it, so that it can be displayed reasonably. If the allowed area is not explicitly set, the user can drag the docking window to any of the four dockable areas.
3. Save the state of the docked window and restore it
Each QObject
can be given an "object name". When creating some docked windows and toolbars, if you want to use QMainWindow ::saveState()
and QMainWindow::restoreStatee()
to save and restore the geometry and state of the docked windows and toolbars.
3.1. Use QSettings to save records
Qt provides the QSetting class to manage the configuration, which can be specified as a configuration file in ini format. If it is not specified, the default is: windows system,
in the registry (registry location: computer\HKEY_CURRENT_USER\Software\software name)
maxos system, in the xml file
unix system, in the ini file
For more information about QSetting, please jump to Qt to read *.ini configuration files through QSttings class
Then we need to provide a pair to store and read the shape and state of the docked window.
#include <QSettings>
void MainWindow::WriteSettings()
{
QSettings setting("Linxi07", "DockWindows");
setting.beginGroup("mainWindow");
setting.setValue("geometry", saveGeometry());
setting.setValue("state", saveState());
setting.endGroup();
}
void MainWindow::ReadSettings()
{
QSettings setting("Linxi07", "DockWindows");
setting.beginGroup("mainWindow");
restoreGeometry(setting.value("geometry").toByteArray());
restoreState(setting.value("state").toByteArray());
setting.endGroup();
}
Also for the convenience of testing, add some status bar codes in the constructor.
QToolBar* frontToolBar = new QToolBar(this);
frontToolBar->setObjectName("frontToolBar");
QComboBox* comboBox = new QComboBox(this);
frontToolBar->addWidget(comboBox);
QSpinBox* spinBox = new QSpinBox(this);
frontToolBar->addWidget(spinBox);
QAction* a1 = new QAction("a1", this);
frontToolBar->addAction(a1);
frontToolBar->setAllowedAreas(Qt::TopToolBarArea | Qt::BottomToolBarArea);
addToolBar(frontToolBar);
Then we need the last step, which is to call. Called in the last line of the constructor ReadSettings()
, and called in the first line of the destructor WriteSettings()
.
3.2 Test results
Here I have recorded a video and you can see that the status bar is on the first page when it is opened for the first time, and the docking window is on the right. When I move the status bar to the lower side, the docked window moves to the left. When you start the program again, you will find that the last operation is retained.
3.3. Registry view configuration
Because the test platform is windows, the configuration is stored in the registry by default, and the path is:计算机\HKEY_CURRENT_USER\Software\Linxi07\DockWindows\mainWindow