環境:Win10+VS2015+Qt5.9.7
首先創建一個Qt GUI Application。工程名:myDockWidget。然後,在資源文件中添加一幅圖像資源:
頭文件
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);
}
效果如圖:
爲停靠窗口中的控件添加槽函數
頭文件
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, "提示:", "開始運行");
}