2024-07-12
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
Table of contents
2. Create a child window by code:
4. Add controls to the child window:
5. Set the sub-window docking position.
In Qt, you can create and manage child windows (child window bodies) to implement multi-window applications.
A child window can be a dialog box, an independent window, or an MDI (multiple document interface) child window.
Create a child window through the QDockWidget class.
- Create a child window object: QDockWidget* dockWidget = new QDockWidget();
- Add the child window to the main window: this->addDockWidget(Qt::LeftDockWidgetArea,dockWidget);
- Set the subwindow title:dockWidget->setWindowTitle("This is the title of the subwindow");
- Add controls to the child window. You cannot directly set child controls for this window.
- First, create a separate QWidget object and then set the controls to this QWidget object.
- Then set this QWidget object to the dockWidget.
//为dockWidget赋予QWidget对象 QWidget* container=new QWidget(); dockWidget->setWidget(container);
- Since a dockWidget can only contain one QWidget object, if you want to add a new control, you still have to add it to the QWidget object.
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); QDockWidget* dockWidget=new QDockWidget(); this->addDockWidget(Qt::LeftDockWidgetArea,dockWidget); dockWidget->setWindowTitle("这是子窗口的标题"); //为dockWidget赋予QWidget对象 QWidget* container=new QWidget(); dockWidget->setWidget(container); //创建两个控件 QLabel* label=new QLabel("这是一个label控件"); QPushButton* pushButton=new QPushButton("这是一个按钮"); //创建一个布局管理利器,并且设置到QWidget对象中 QVBoxLayout* layout=new QVBoxLayout(); container->setLayout(layout); //将两个控件添加到布局管理器中 layout->addWidget(label); layout->addWidget(pushButton); }
- Use the setAllowAreas(Qt::DockWidgetArea | Qt::DockWidgetArea) method to set the allowed docking location. The default is Qt::LeftDockWidgetArea - left.
- Four positions can be set, the default position is top:
- Qt::TopDockWidgetArea——上
- Qt::BottomDockWidgetArea——下
- Qt::LeftDockWidgetArea——左
- Qt::RightDockWidgetArea——右