Fork me on GitHub

Qt之布局管理——停靠窗口

QDockWidget类继承与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);	//把edt设置为中心控件
	
	//停靠窗口1:
	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);

	//停靠窗口2:
	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);

	//停靠窗口3:
	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。


posted @ 2012-11-16 22:21  韩兆新  阅读(12178)  评论(2编辑  收藏  举报