Qt之停靠窗口QDockWidget类
设置停靠窗口的一般流程 1、创建一个QDockWidget对象的停靠窗口 2、设置此停靠窗体的属性,通常调用setFeatures()以及setAllowAreas()两种方法 3、新建一个要插入停靠窗口的控件,常用的有QListWidget和QTextEdit 4、将控件插入停靠窗口,调用QDockWidget的setWidget()方法 5、使用addDockWidget方法在MainWindow中加入此停靠窗口步骤:
1、新建Qt GUI应用,项目名称DockWindows,基类选择"QMainWindow",类名选择"DockWindows",取消"创建界面" 2、修改DockWindows.h,DockWindows.cpp文件,在构造函数中实现窗口的初始化以及功能实现DockWindows.h
#ifndef DOCKWINDOWS_H#define DOCKWINDOWS_H#includeclass DockWindows : public QMainWindow{ Q_OBJECTpublic: explicit DockWindows(QWidget *parent = 0); ~DockWindows();};#endif // DOCKWINDOWS_H
DockWindows.cpp
#include "dockwindows.h"#include "ui_dockwindows.h"#include#include DockWindows::DockWindows(QWidget *parent) : QMainWindow(parent){ setWindowTitle(tr("windows")); //设置主窗口的标题栏文字 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); //可关闭、可浮动 // QDockWidget::NoDockWidgetFeatures //不可以关闭,不可以移动,不可以浮动 dock->setAllowedAreas(Qt::TopDockWidgetArea | Qt::BottomDockWidgetArea); //顶端停靠,底部停靠 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(){}
---