Qt dock windows and toolbars

A docked window is a window that can be docked in QMainWindow or floated as an independent window. QMainWindow provides 4 docking window areas: upper, lower, left and right. In Qt, every docked window is an instance of QDockWidget.

 

Each docking window has its own title bar. By dragging the title bar, the user can move the docking window to different docking areas. When the docking window is dragged outside the docking area, the docking window can be moved from a docking area. Detach and become an independent window.

 

In earlier versions of Qt, toolbars were handled in the same way as docked windows and shared the same docking area. Since Qt4, the toolbar occupies its own area and is set to not be undocked. If you want to add a floating toolbar, you need to put it on the QDockWidget.

The docking area and toolbar area of ​​QMainWindow are shown in the following figure:

The four corners shown by the dotted line can belong to either of the two adjacent docking areas, for example QMainWindow::setCorner(Qt::TopLeftCorner, Qt::LeftDockWidgetArea) sets the upper left corner to belong to the left docking area.

 

The following program fragment encapsulates an existing widget in QDockWidget and inserts it into the docking area on the right:

//设置容器
QDockWidget* dock_widget(new QDockWidget(tr("Shape")));
dock_widget->setObjectName(tr("ShapeDockWidget"));

//将窗口控件添加到容器中
dock_widget->setWidget(new Widget());
//设置容器在窗口的可放置区
dock_widget->setAllowedAreas(Qt::LeftDockWidgetArea |
                             Qt::TopDockWidgetArea |
                             Qt::BottomDockWidgetArea);
//将容器放置到窗口的上部
addDockWidget(Qt::TopDockWidgetArea, dock_widget);

 

Generated as follows:

 

Similarly, the toolbar also has the following settings, the user can place the toolbar in different areas.

//给工具栏添加QAction
ui->mainToolBar->addAction(actionOpen);
ui->mainToolBar->addAction(actionSave);

//设置工具栏的名称
ui->mainToolBar->setObjectName(tr("Tool Bar"));

//设置工具栏在窗口组件中的放置位置,上部和左侧
ui->mainToolBar->setAllowedAreas(Qt::TopToolBarArea | Qt::LeftToolBarArea);

//因为是QtCreator自动生成的QMainWindow窗口,所以不需要讲工具栏再添加到窗口中。

The effect diagram is as follows:

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325924180&siteId=291194637