//main.cpp
#include "dockwindows.h"#include <QApplication>int main(int argc, char *argv[]){ QApplication a(argc, argv); DockWindows w; w.show(); return a.exec();}
//dockwindow.h
#ifndef DOCKWINDOWS_H
#define DOCKWINDOWS_H
#include <QMainWindow>
class DockWindows : public QMainWindow
{
Q_OBJECT
public:
DockWindows(QWidget *parent = 0);
~DockWindows();
};
#endif // DOCKWINDOWS_H
//dockwindow.cpp
#include "dockwindows.h"
#include<QTextEdit>
#include<QDockWidget>
DockWindows::DockWindows(QWidget *parent)
: QMainWindow(parent)
{
setWindowTitle(tr("DockWindows"));//设置主窗口的标题文字
QTextEdit *te=new QTextEdit(this);//定义一个QTextEdit对象作为主窗口
te->setText(tr("Main Window"));
te->setAlignment(Qt::AlignCenter);
setCentralWidget(te); //将此编辑框设为主窗口的*窗体
//停靠窗口1
QDockWidget *dock=new QDockWidget(tr("DockWindow1"),this);
dock->setFeatures(QDockWidget::DockWidgetMovable); //可移动
dock->setAllowedAreas(Qt::LeftDockWidgetArea|Qt::RightDockWidgetArea);
QTextEdit *te1 =new QTextEdit();
te1->setText(tr("Window1,The dock widget can be moved between docks by the user" ""));
dock->setWidget(te1);
addDockWidget(Qt::RightDockWidgetArea,dock);
//停靠窗口2
dock=new QDockWidget(tr("DockWindow2"),this);
dock->setFeatures(QDockWidget::DockWidgetClosable|QDockWidget::DockWidgetFloatable); //可关闭,可浮动
QTextEdit *te2 =new QTextEdit();
te2->setText(tr("Window2,The dock widget can be detached from the main window,""and floated as an independent window, and can be closed"));
dock->setWidget(te2);
addDockWidget(Qt::RightDockWidgetArea,dock);
//停靠窗口3
dock=new QDockWidget(tr("DockWindow3"),this);
dock->setFeatures(QDockWidget::AllDockWidgetFeatures); //全部特性
QTextEdit *te3 =new QTextEdit();
te3->setText(tr("Window3,The dock widget can be closed, moved, and floated"));
dock->setWidget(te3);
addDockWidget(Qt::RightDockWidgetArea,dock);
}
DockWindows::~DockWindows()
{
}
停靠窗口,顾名思义,便是停靠在界面边缘的一些窗口
值得注意的是:
dock->setFeatures(QDockWidget::DockWidgetMovable); //可移动
dock->setAllowedAreas(Qt::LeftDockWidgetArea|Qt::RightDockWidgetArea);通过这两个函数,设置dock的一些属性,
第一个表示设置停靠窗口的属性,比如可移动,浮动,等等
第二个表示设置停靠窗口的停留位置,左边,右边