Qt布局管理: 停靠窗口QDockWidget类(纯代码实现)
Qt布局管理,停靠窗口,QDockWidget类,纯代码实现2016-05-29
(注:以下解释是我自己的翻译,由于英文水平有限,望海涵)
详细描述:
QDockWidget类提供了一个窗体部件,其可以停靠在QMainWindow,或其本身作为一个在桌面上的顶级窗口(也就是父窗体)。
QDockWidget类提供了dock widget的概念,dock widget也就是我们熟悉的工具面板或者是工具窗口。
Dock window是放置在QMainWindow窗口周围的dock widget区域中的小部件。
原文:
Detailed Description
The QDockWidget class provides a widget that can be docked inside a QMainWindow or floated as a top-level window on the desktop.
QDockWidget provides the concept of dock widgets, also know as tool palettes or utility windows. Dock windows are secondary windows placed in the dock widget area around the central widget in a QMainWindow.
图形描述如下:
Dock windows可以在几个区域中移动,或者是悬浮。该类提供的API允许程序员限制dock widgets的移动,悬浮和关闭,以及它可以被放置的区域。
原文:
Dock windows can be moved inside their current area, moved into new areas and floated (e.g., undocked) by the end-user. The QDockWidget API allows the programmer to restrict the dock widgets ability to move, float and close,
as well as the areas in which they can be placed.
成员类型:
QDockWidget对象包含一些特性,例如:移动、悬浮、关闭等等,这些都是枚举类型。
创建停靠窗体的流程:
实例:
步骤:
1、新建GUI应用,项目名自定义,基类选择QMainWindow,注意要取消"创建界面"复选框。
MainWindow类构造函数的说明:
class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = 0); ~MainWindow(); };
#include "mainwindow.h" #include <QTextEdit> #include <QDockWidget> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { setWindowTitle(tr("DockWidget")); QTextEdit *text = new QTextEdit(this); text->setText(tr("主窗口")); text->setAlignment(Qt::AlignCenter); setCentralWidget(text); QDockWidget *dock = new QDockWidget(tr("DockWindow1"),this); dock->setFeatures(QDockWidget::DockWidgetMovable); //指定停靠窗体的样式,此处为可移动 dock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea); QTextEdit *text1 = new QTextEdit(); text1->setText(tr("窗口一")); dock->setWidget(text1); addDockWidget(Qt::RightDockWidgetArea,dock); dock = new QDockWidget(tr("DockWindow2"),this); dock->setFeatures(QDockWidget::NoDockWidgetFeatures); //没有相关的权限 QTextEdit *text2 = new QTextEdit(); text2->setText(tr("窗口二")); dock->setWidget(text2); addDockWidget(Qt::RightDockWidgetArea,dock); dock = new QDockWidget(tr("DockWindow3"),this); dock->setFeatures(QDockWidget::AllDockWidgetFeatures); //具有全部特性 QTextEdit *text3 = new QTextEdit(); text3->setText(tr("窗口三")); dock->setWidget(text3); addDockWidget(Qt::RightDockWidgetArea,dock); } MainWindow::~MainWindow() { }
#include "mainwindow.h" #include <QApplication> #include <QFont> int main(int argc, char *argv[]) { QApplication a(argc, argv); QFont font("楷体",20,QFont::Bold); a.setFont(font); MainWindow w; w.show(); w.setFixedSize(600,400); //设置窗体为固定的大小,防止用户通过拖拽改变其大小 return a.exec(); }