DockWidget類繼承與QWidget類,用於停靠視窗的管理。在主視窗中,先設定中心控制元件,然後例項化 QDockWidget物件,通過setFeatures( )設定停靠視窗的窗體特性,通過setAllowedAreas( )設定窗體可停靠的區域。具體如下:
void
setFeatures(DockWidgetFeatures features ) |
QDockWidget::DockWidgetClosable |
停靠窗可關閉 |
QDockWidget::DockWidgetMovable |
停靠窗可移動 |
QDockWidget::DockWidgetFloatable |
停靠窗可浮動 |
QDockWidget::DockWidgetVerticalTitleBar |
標題欄垂直放置 |
QDockWidget::AllDockWidgetFeatures |
可關閉、可移動、可浮動 |
QDockWidget::NoDockWidgetFeatures |
不可關閉、不可移動、不可浮動 |
void
setAllowedAreas ( Qt::DockWidgetAreas areas ) |
Qt::LeftDockWidgetArea |
可在中央控制元件的左側停靠 |
Qt::RightDockWidgetArea |
可在中央控制元件的右側停靠 |
Qt::TopDockWidgetArea |
可在中央控制元件的頂部停靠 |
Qt::BottomDockWidgetArea |
可在中央控制元件的底部停靠 |
Qt::AllDockWidgetAreas |
可在中央控制元件的任意方向停靠 |
Qt::NoDockWidgetArea |
只可停靠在插入處 |
dockwindows.h
#ifndef DOCKWINDOWS_H
|
#define DOCKWINDOWS_H
|
|
#include <QtGui>
|
|
class
DockWindows : public QMainWindow
|
{ |
Q_OBJECT
|
|
public:
|
DockWindows(QWidget
*parent =
0, Qt::WFlags flags
= 0); |
~DockWindows();
|
}; |
|
#endif // DOCKWINDOWS_H |
dockwindows.cpp
#include
"dockwindows.h" |
#include <QDockWidget>
|
DockWindows::DockWindows(QWidget
*parent, Qt::WFlags flags)
|
: QMainWindow(parent, flags)
|
{ |
setWindowTitle(tr("Dock Windows"));
|
QTextEdit
*edt = new
QTextEdit(tr("Main Window"),this);
|
setCentralWidget(edt);
|
|
|
QDockWidget
*dockWidget1 = new
QDockWidget(tr("Dock Window 1"),this);
|
dockWidget1->setFeatures(QDockWidget::DockWidgetMovable);
|
dockWidget1->setAllowedAreas(Qt::LeftDockWidgetArea
| Qt::RightDockWidgetArea);
|
QTextEdit
*edt1 = new
QTextEdit(tr("Window 1"));
|
dockWidget1->setWidget(edt1);
|
addDockWidget(Qt::RightDockWidgetArea,dockWidget1);
|
|
|
QDockWidget
*dockWidget2 = new
QDockWidget(tr("Dock Window 2"),this);
|
dockWidget2->setFeatures(QDockWidget::DockWidgetFloatable
| QDockWidget::DockWidgetClosable);
|
QTextEdit
*edt2 = new
QTextEdit(tr("Window 2"));
|
dockWidget2->setWidget(edt2);
|
addDockWidget(Qt::LeftDockWidgetArea,dockWidget2);
|
|
|
QDockWidget
*dockWidget3 = new
QDockWidget(tr("Dock Window 3"),this);
|
dockWidget3->setFeatures(QDockWidget::AllDockWidgetFeatures);
|
QTextEdit
*edt3 = new
QTextEdit(tr("Window 3"));
|
dockWidget3->setWidget(edt3);
|
addDockWidget(Qt::BottomDockWidgetArea,dockWidget3);
|
|
} |
|
DockWindows::~DockWindows()
|
{ |
|
} |
在本例中還用到了QMainWindow類的addDockWidget()方法。使用情況請查閱QtAssistant。