天天看點

QT界面:QDockWidget停靠視窗使用小結

QDockWidget停靠視窗

Qt建構停靠視窗使用的是QDockWidget類。

視窗特性

停靠視窗特性可以通過setFeatures(QDockWidget::AllDockWidgetFeatures)方法進行設定:

常亮 描述
QDockWidget::DockWidgetClosable 0x01 視窗可關閉
QDockWidget::DockWidgetMovable 0x02 視窗可移動
QDockWidget::DockWidgetFloatable 0x04 停靠視窗可以與主視窗分離,作為獨立視窗浮動
QDockWidget::DockWidgetVerticalTitleBar 0x08 停靠視窗在左側顯示垂直标題欄。 用于增加QMainWindow中的垂直空間。
QDockWidget::AllDockWidgetFeatures DockWidgetClosable /DockWidgetMovable / DockWidgetFloatable (不建議使用)可以關閉,移動和浮動停靠視窗
QDockWidget::NoDockWidgetFeatures 0x00 停靠視窗不可關閉、移動、浮動

停靠區域

通過setAllowedAreas(Qt::DockWidgetAreas areas)設定視窗停靠區域:

常亮
Qt::LeftDockWidgetArea 0x1
Qt::RightDockWidgetArea 0x2
Qt::TopDockWidgetArea 0x4
Qt::BottomDockWidgetArea 0x8
Qt::AllDockWidgetAreas DockWidgetArea_Mask
Qt::NoDockWidgetArea

隐藏與顯示

視窗的隐藏與顯示可通過hide()、show()實作,判斷視窗的顯示與隐藏狀态可用isHidden()。

示例

環境:Win10+VS2015+Qt5.9.7

首先建立一個Qt GUI Application。工程名:myDockWidget。然後,在資源檔案中添加一幅圖像資源:

QT界面:QDockWidget停靠視窗使用小結

頭檔案

myDockWidget.h:

#pragma once

#include <QtWidgets/QMainWindow>
#include <QHBoxLayout>
#include <QPushButton>
#include <QRadioButton>
#include <QCheckBox>
#include <QSpinBox>
#include <QLabel>
#include <QTextEdit>
#include <QDockWidget>

#include "ui_myDockWidget.h"

#pragma execution_character_set("utf-8")

class myDockWidget : public QMainWindow
{
	Q_OBJECT

public:
	myDockWidget(QWidget *parent = Q_NULLPTR);

private:
	Ui::myDockWidgetClass ui;
};

           

源檔案

myDockWidget.cpp:

在停靠視窗中添加一些控件資源。

#include "myDockWidget.h"

myDockWidget::myDockWidget(QWidget *parent)
	: QMainWindow(parent)
{
	ui.setupUi(this);

	//顯示——停靠視窗
	QLabel *m_qLabel = new QLabel("", this);
	m_qLabel->setAlignment(Qt::AlignCenter);
	m_qLabel->setStyleSheet("border-image: url(:/myDockWidget/Resources/image.jpg);");
	m_qLabel->setMinimumWidth(500);
	m_qLabel->setMinimumHeight(300);

	QHBoxLayout *hlayout0 = new QHBoxLayout;
	hlayout0->addWidget(m_qLabel);
	QGridLayout *vlayout0 = new QGridLayout;
	vlayout0->addLayout(hlayout0, 0, 0);

	QWidget *cornerWidget0 = new QWidget;
	cornerWidget0->setLayout(vlayout0);
	setCentralWidget(cornerWidget0);

	//調試——停靠視窗
	QDockWidget *debugging = new QDockWidget("調試", this);//建構停靠視窗,指定父類
	debugging->setFeatures(QDockWidget::DockWidgetMovable | QDockWidget::DockWidgetClosable);//設定停靠視窗特性,可移動,可關閉
	debugging->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);//設定可停靠區域為主視窗左邊和右邊
	debugging->setMinimumWidth(200);

	QTextEdit *debuggingEdit = new QTextEdit("調試——停靠視窗);
	debugging->setWidget(debuggingEdit);
	addDockWidget(Qt::RightDockWidgetArea, debugging);

	//測試——停靠視窗
	QDockWidget *dw3 = new QDockWidget("測試", this);//建構停靠視窗,指定父類
	dw3->setFeatures(QDockWidget::DockWidgetMovable);//設定停靠視窗特性,可移動,可關閉
	dw3->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);//設定可停靠區域為主視窗左邊和右邊

	QHBoxLayout *hlayout31 = new QHBoxLayout;
	QPushButton *runPushButton = new QPushButton(tr("運作"));
	QPushButton *stopPushButton = new QPushButton(tr("停止"));
	hlayout31->addWidget(runPushButton);
	hlayout31->addWidget(stopPushButton);

	QGridLayout *vlayout3 = new QGridLayout;
	vlayout3->addLayout(hlayout31, 0, 0);
	QWidget *cornerWidget3 = new QWidget;
	cornerWidget3->setLayout(vlayout3);
	dw3->setWidget(cornerWidget3);
	dw3->setMaximumHeight(100);
	dw3->setMaximumWidth(300);
	addDockWidget(Qt::LeftDockWidgetArea, dw3);

}

           

效果如圖:

QT界面:QDockWidget停靠視窗使用小結

為停靠視窗中的控件添加槽函數

頭檔案

myDockWidget.h:

#pragma once

#include <QtWidgets/QMainWindow>
#include <QHBoxLayout>
#include <QPushButton>
#include <QRadioButton>
#include <QCheckBox>
#include <QSpinBox>
#include <QLabel>
#include <QTextEdit>
#include <QDockWidget>
#include <QMessageBox>

#include "ui_myDockWidget.h"

#pragma execution_character_set("utf-8")

class myDockWidget : public QMainWindow
{
	Q_OBJECT

public:
	myDockWidget(QWidget *parent = Q_NULLPTR);

private:
	Ui::myDockWidgetClass ui;
	QPushButton *runPushButton;

private slots:
	void runPushButtonSlot();

};

           

源檔案

myDockWidget.cpp:

#include "myDockWidget.h"

myDockWidget::myDockWidget(QWidget *parent)
	: QMainWindow(parent)
{
	ui.setupUi(this);

	//顯示——停靠視窗
	QLabel *m_qLabel = new QLabel("", this);
	m_qLabel->setAlignment(Qt::AlignCenter);
	m_qLabel->setStyleSheet("border-image: url(:/myDockWidget/Resources/image.jpg);");
	m_qLabel->setMinimumWidth(500);
	m_qLabel->setMinimumHeight(300);

	QHBoxLayout *hlayout0 = new QHBoxLayout;
	hlayout0->addWidget(m_qLabel);
	QGridLayout *vlayout0 = new QGridLayout;
	vlayout0->addLayout(hlayout0, 0, 0);

	QWidget *cornerWidget0 = new QWidget;
	cornerWidget0->setLayout(vlayout0);
	setCentralWidget(cornerWidget0);

	//調試——停靠視窗
	QDockWidget *debugging = new QDockWidget("調試", this);//建構停靠視窗,指定父類
	debugging->setFeatures(QDockWidget::DockWidgetMovable | QDockWidget::DockWidgetClosable);//設定停靠視窗特性,可移動,可關閉
	debugging->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);//設定可停靠區域為主視窗左邊和右邊
	debugging->setMinimumWidth(200);

	QTextEdit *debuggingEdit = new QTextEdit("調試——停靠視窗");
	debugging->setWidget(debuggingEdit);
	addDockWidget(Qt::RightDockWidgetArea, debugging);

	//測試——停靠視窗
	QDockWidget *dw3 = new QDockWidget("測試", this);//建構停靠視窗,指定父類
	dw3->setFeatures(QDockWidget::DockWidgetMovable);//設定停靠視窗特性,可移動,可關閉
	dw3->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);//設定可停靠區域為主視窗左邊和右邊

	QHBoxLayout *hlayout31 = new QHBoxLayout;
	runPushButton = new QPushButton(tr("運作"));
	runPushButton->setStyleSheet("background-color: rgb(0, 255, 127);");
	QObject::connect(runPushButton, SIGNAL(clicked()), this, SLOT(runPushButtonSlot()));

	QPushButton *stopPushButton = new QPushButton(tr("停止"));

	hlayout31->addWidget(runPushButton);
	hlayout31->addWidget(stopPushButton);

	QGridLayout *vlayout3 = new QGridLayout;
	vlayout3->addLayout(hlayout31, 0, 0);
	QWidget *cornerWidget3 = new QWidget;
	cornerWidget3->setLayout(vlayout3);
	dw3->setWidget(cornerWidget3);
	dw3->setMaximumHeight(100);
	dw3->setMaximumWidth(300);
	addDockWidget(Qt::LeftDockWidgetArea, dw3);

}

void myDockWidget::runPushButtonSlot()
{
	runPushButton->setStyleSheet("background-color: rgb(255, 100, 100);");
	QMessageBox::information(this, "提示:", "開始運作");
}

           
QT界面:QDockWidget停靠視窗使用小結
QT界面:QDockWidget停靠視窗使用小結

繼續閱讀