1 // 3.2 停靠窗口 QDockWidget类
2 setFeatures()
3 setAllowedAreas()
4 setWidget()
5 addDockWidget()
6
7 //dockwindows.cpp
8 #include "dockwindows.h"
9 #include <QTextEdit>
10 #include <QDockWidget>
11 DockWindows::DockWindows(QWidget *parent)
12 : QMainWindow(parent)
13 {
14 setWindowTitle(tr("DockWindows"));
15 QTextEdit *te=new QTextEdit(this);//设置为主窗口
16 te->setText(tr("Main Window"));
17 te->setAlignment(Qt::AlignCenter);
18 setCentralWidget(te);//设置为主窗口的中央窗体
19
20 QDockWidget *dock= new QDockWidget(tr("DockWindow1"),this);
21 dock->setFeatures(QDockWidget::DockWidgetMovable);
22 dock->setAllowedAreas(Qt::LeftDockWidgetArea|Qt::RightDockWidgetArea);
23 QTextEdit *te1=new QTextEdit();
24 te1->setText(tr("Windows1 This dock widget can be moved between docks by the user"));
25 dock->setWidget(te1);//将控件加入dock停靠窗体
26 addDockWidget(Qt::RightDockWidgetArea,dock);//在MainWindow中加入此停靠窗体
27
28 dock= new QDockWidget(tr("DockWindow2"),this);
29 dock->setFeatures(QDockWidget::DockWidgetClosable|QDockWidget::DockWidgetFloatable);
30 QTextEdit *te2 = new QTextEdit();
31 te2->setText(tr("Windows2 The dock widget can be detached from the main window,and floated as an independent window, and can be closed"));
32 dock->setWidget(te2);
33 addDockWidget(Qt::RightDockWidgetArea,dock);
34
35 dock=new QDockWidget(tr("DockWindow3"),this);
36 dock->setFeatures(QDockWidget::AllDockWidgetFeatures);
37 QTextEdit *te3=new QTextEdit();
38 te3->setText(tr("Windows3 The dock widget can be closed, moved, and floated"));
39 dock->setWidget(te3);
40 addDockWidget(Qt::RightDockWidgetArea,dock);
41 }
42
43 DockWindows::~DockWindows()
44 {
45
46 }