QDockWidget提供了dock widget的概念,也称为工具面板或实用工具窗口。停靠窗口是放置在QMainWindow中央小部件周围的停靠小部件区域中的次要窗口。许多工程软件,如Qt Creator,VS,matlab等,都是使用dock布局窗口,这样用户可以自定义界面,自由组合窗口。
Qt的嵌套布局由QDockWidget完成,用Qt Creator拖界面得到的dock布置形式比较固定,不能得想要的任意组合形式,要得到如下图所示的效果,后续布局必须通过代码来完成。
如图所示:
1.新建QMainWindow基类工程,在ui设计页面拖入3个QDockWidget控件。
也可以不在UI设计器里面加,使用代码加入QDockWidget控件
2.因为整个窗口都由dock组成,所以在myMainWindow的构造函数中先把QMainWindow的中间窗口部件去除。
QWidget* central_form = takeCentralWidget();
if(central_form)
delete central_form;
3.用setWindowTitle(const QString &)函数为每个dock添加标题便于后面区分。
this->setWindowTitle(tr("图像处理系统"));
4.接下来配置DockWidget嵌套
setDockNestingEnabled(true);
这句语句是用来配置dock是否可以嵌套。通过查询帮助文档,我们可以知道,如果参数为false,则停靠区域只能水平或者垂直,即dock不能放置在中间,只能在两侧排布。如果参数为true,那么dock所占用的区域可以向任意方向排布。
接下来先移除所有的dock,我们来调整dock位置。调整dock位置前通过
void MainFramework::dockLayout() {
this->addDockWidget(Qt::LeftDockWidgetArea, m_projManagerView, Qt::Orientation::Vertical);
this->addDockWidget(Qt::RightDockWidgetArea, m_propertyView, Qt::Orientation::Vertical);
this->addDockWidget(Qt::BottomDockWidgetArea, m_logView, Qt::Orientation::Vertical);
}
5.为第一个dock指定位置,接下来通过配置的参数对所有的dock进行排列,这个函数是将第一个停靠dock覆盖的空间分成两部分,将第一个停靠dock移动到第一部分中,并将第二个停靠dock移动到第二部分中。通过这样的方式可以先左右,再上下的原则依次对dock进行排列。例如:
void QMainWindow::splitDockWidget(QDockWidget *first, QDockWidget *second, Qt::Orientation orientation)
1.配置dock最小的宽度、高度,最大尺寸。
m_logView->setMinimumWidth(150);
m_propertyView->setMinimumHeight(100);
m_projManagerView->setMaximumSize(100,100);
2.隐藏标题栏。
QWidget* TitleBar = m_propertyView->titleBarWidget();
QWidget* EmptyWidget = new QWidget();
m_propertyView->setTitleBarWidget(EmptyWidget);
delete TitleBar;
3.合并窗口,采用void QMainWindow::tabifyDockWidget(QDockWidget *first, QDockWidget *second)函数,将多个QDockWidget叠加到同一区域。
tabifyDockWidget(ui->dockWidget_4,ui->dockWidget_5);
m_propertyView->setFeatures(QDockWidget::DockWidgetFloatable);
效果如下:
#ifndef MAINFRAMEWORK_H
#define MAINFRAMEWORK_H
#include
#include
#include
#include
#include
#include "FlyPropertyWin.h"
#include "ProjectWin.h"
#include "MsgBox.h"
QT_BEGIN_NAMESPACE
namespace Ui { class MainFramework; }
QT_END_NAMESPACE
class MainFramework : public QMainWindow
{
Q_OBJECT
public:
MainFramework(QWidget *parent = nullptr);
~MainFramework();
private slots:
void addLog(const QString&);
//标题栏槽函数
void s_showToolBar();
void s_showPorjectWin();
void s_showPropertyWin();
void s_showLogWin();
void s_close();
void s_min();
void s_hideProj();
void s_propertyWinHide(bool);
void s_logWinHide(bool);
void s_timeout();
void s_newProject();
void s_saveFile();
void s_runAc();
void s_stopAc();
void s_debugAc();
void s_anaysisAc();
void s_recordAc();
void s_grabAc();
void s_capacityAc();
void s_paramterAc();
void s_publishAc();
void s_exportAc();
private:
void initTitleBar();
void initLogView();
void initWorkSpaceView();
void initPropertyView();
void initProjectView();
void initToolBar();
void dockLayout();
void initMaxMinWin();
void showMessageBox(const QString&);
private:
Ui::MainFramework *ui;
//标题栏
QAction* m_toolBarAc = NULL;
QAction* m_projectAc = NULL;
QAction* m_logAc = NULL;
QAction* m_propertyAc = NULL;
//工具栏
QAction* m_newAc = NULL;
QAction* m_saveAc = NULL;
QAction* m_runAc = NULL;
QAction* m_stopAc = NULL;
QAction* m_debugAc = NULL;
QAction* m_anaysisAc = NULL;
QAction* m_recordAc = NULL;
QAction* m_grabAc = NULL;
QAction* m_capacityAc = NULL;
QAction* m_paramterAc = NULL;
QAction* m_publishAc = NULL;
QAction* m_exportAc = NULL;
QDockWidget* m_logView = NULL;
QDockWidget* m_propertyView = NULL;
QDockWidget* m_projManagerView = NULL;
QTextEdit* m_logBody = NULL;
QWidget* m_maxminWin = NULL;
QWidget* m_workspace = NULL;
ProjectWin* m_projectWin = NULL;
FlyPropertyWin* m_propertyWin = NULL;
MsgBox* m_msgBox = NULL;
QPushButton* m_minBtn = NULL;
QPushButton* m_closeBtn = NULL;
};
#endif // MAINFRAMEWORK_H
#include "MainFramework.h"
#include "ui_MainFramework.h"
#include
#include
#include
#include
#include
#include
#pragma execution_character_set("utf-8")
Q_DECLARE_METATYPE(QDockWidget::DockWidgetFeatures)
QString MenuBarStyle =
"QMenuBar{background-color:#FFFFFF; font:14px; color:#232323;}"
"QMenuBar::item{\
min-height:40px; \
margin:1 10 0 10px; \
padding:10 10 10 10px; /* 条目内边框 */ \
background:#FFFFFF; /* 背景颜色 */ \
border-radius:4px; /* 倒角 */ \
}"
"QMenuBar::item:selected{background: #E5E5E5; }"
;
QString MenuStyle =
"QMenu{/*整个背景*/}"
"QMenu::item{ \
font-size: 14px; \
color: rgb(225,225,225); /*字体颜色*/\
border: 3px solid rgb(60,60,60); /*item选框*/\
background-color:rgb(89,87,87); \
padding:16px 16px; /*设置菜单项文字上下和左右的内边距,效果就是菜单中的条目左右上下有了间隔*/\
margin:0px 2px;/*设置菜单项的外边距*/\
}"
"QMenu::item:selected{background-color:#1E1E1E;/*选中的样式*/}"
"QMenu::item:pressed{/*菜单项按下效果*/ border: 1px solid rgb(60,60,61); background-color: rgb(220,80,6);}"
;
QString tabBarStyle =
"QTabBar::tab{min-height: 28px; min-width:80px ; font:14px; }"
"QTabBar::tab:!selected{}"
"QTabBar::tab:!selected:hover{ background-color: #d9fffe; color: black;}"
"QToolBar{background-color:#e5e5e5; margin:3px;}"
"QToolBar::separator{height:1px; background-color:#000000;}"
"QToolBar QToolButton{min-width:60px; min-height:40px; background-color:transparent;}"
"QToolBar QToolButton:hover{background-color:rgba(255,255,255,0.8);}"
"QToolBar QToolButton:pressed{background-color:rgba(255,255,255,0.5);}"
;
MainFramework::MainFramework(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainFramework)
{
qRegisterMetaType<QDockWidget::DockWidgetFeatures>();
ui->setupUi(this);
this->setWindowTitle(tr("图像处理系统"));
this->setWindowIcon(QIcon(":/images/logo.png"));
this->setWindowFlags(Qt::Window | Qt::FramelessWindowHint);
this->setStyleSheet(tabBarStyle);
//this->setStyleSheet("QMainWindow::separator {width: 2px;height: 2px;margin: 2px;padding: 2,2px;}");
//On the top menu bar add toolbar,menubar and dockwidget.
initTitleBar();
initToolBar();
initLogView();
initProjectView(); //项目区初始化在前。
initWorkSpaceView(); //工作区初始化在后。
initPropertyView();
dockLayout();
this->setTabPosition(Qt::AllDockWidgetAreas, QTabWidget::North);
//This property holds the tab shape used for tabbed dock widgets.
this->setTabShape(QTabWidget::Triangular);
this->statusBar()->showMessage(tr("Status Bar"));
addLog("启动完成,欢迎进入主框架!");
QTimer::singleShot(2, this, SLOT(s_timeout()));
}
void MainFramework::initWorkSpaceView() {
if (NULL == m_workspace) {
m_workspace = new QWidget;
m_workspace->setWindowTitle("工作区");
this->setCentralWidget(m_workspace);
}
qDebug() << "工作区 id:" << m_workspace->winId() << ", name:" << m_workspace->windowTitle();
}
void MainFramework::initTitleBar()
{
QMenuBar* m_MenuBar = this->menuBar();
m_MenuBar->setStyleSheet(MenuBarStyle);
QMenu* m_viewMenu = this->menuBar()->addMenu(tr("快视"));
m_viewMenu->setStyleSheet(MenuStyle);
m_toolBarAc = new QAction("工具栏");
connect(m_toolBarAc, &QAction::triggered, this, &MainFramework::s_showToolBar);
m_toolBarAc->setCheckable(true);
m_toolBarAc->setChecked(true);
m_projectAc = new QAction("图像列表");
connect(m_projectAc, &QAction::triggered, this, &MainFramework::s_showPorjectWin);
m_projectAc->setCheckable(true);
m_projectAc->setChecked(true);
m_propertyAc = new QAction("属性");
connect(m_propertyAc, &QAction::triggered, this, &MainFramework::s_showPropertyWin);
m_propertyAc->setCheckable(true);
m_propertyAc->setChecked(true);
m_logAc = new QAction("输出");
connect(m_logAc, &QAction::triggered, this, &MainFramework::s_showLogWin);
m_logAc->setCheckable(true);
m_logAc->setChecked(true);
m_viewMenu->addAction(m_toolBarAc);
m_viewMenu->addAction(m_projectAc);
m_viewMenu->addAction(m_propertyAc);
m_viewMenu->addAction(m_logAc);
QMenu* m_setView = this->menuBar()->addMenu(tr("浏览"));
QMenu* m_toolMenu = this-> menuBar()->addMenu(tr("SAR处理"));
QMenu* m_helpMenu = this->menuBar()->addMenu(tr("帮助"));
initMaxMinWin();
}
void MainFramework::initMaxMinWin() {
m_maxminWin = new QWidget(this);
QHBoxLayout* horizontalLayout;
QSpacerItem* horizontalSpacer;
horizontalLayout = new QHBoxLayout(m_maxminWin);
horizontalLayout->setSpacing(0);
horizontalLayout->setContentsMargins(11, 11, 11, 11);
horizontalLayout->setObjectName(QString::fromUtf8("horizontalLayout"));
horizontalLayout->setContentsMargins(0, 0, 0, 0);
horizontalSpacer = new QSpacerItem(241, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
m_minBtn = new QPushButton(m_maxminWin);
connect(m_minBtn, &QPushButton::clicked, this, &MainFramework::s_min);
m_minBtn->setObjectName(QString::fromUtf8("minBtn"));
m_minBtn->setMinimumSize(QSize(40, 40));
m_minBtn->setMaximumSize(QSize(40, 40));
m_minBtn->setStyleSheet(QString::fromUtf8("QPushButton{border:none;}\n"
"QPushButton::hover{background-color:#E5E5E5;}\n"
""));
QIcon icon;
icon.addFile(QString::fromUtf8(":/images/min_main.png"), QSize(), QIcon::Normal, QIcon::Off);
m_minBtn->setIcon(icon);
m_minBtn->setIconSize(QSize(25, 25));
m_minBtn->setAutoDefault(false);
m_minBtn->setFlat(false);
m_minBtn->setDefault(false);
m_closeBtn = new QPushButton(m_maxminWin);
connect(m_closeBtn, &QPushButton::clicked, this, &MainFramework::s_close);
m_closeBtn->setObjectName(QString::fromUtf8("closeBtn"));
m_closeBtn->setMinimumSize(QSize(40, 40));
m_closeBtn->setMaximumSize(QSize(40, 40));
m_closeBtn->setStyleSheet(QString::fromUtf8("QPushButton{border:none;}\n"
"QPushButton::hover{background-color:#E81123;}\n"
));
QIcon icon1;
icon1.addFile(QString::fromUtf8(":/images/close_release.png"), QSize(), QIcon::Normal, QIcon::Off);
m_closeBtn->setIcon(icon1);
m_closeBtn->setIconSize(QSize(20, 20));
m_closeBtn->setFlat(false);
horizontalLayout->addItem(horizontalSpacer);
horizontalLayout->addWidget(m_minBtn);
horizontalLayout->addWidget(m_closeBtn);
horizontalLayout->setStretch(0, 1);
m_maxminWin->setLayout(horizontalLayout);
this->menuBar()->setCornerWidget(m_maxminWin, Qt::TopRightCorner);
}
void MainFramework::s_showToolBar() {
if (m_toolBarAc->isChecked()) {
ui->mainToolBar->show();
addLog("显示 工具栏");
}
else {
ui->mainToolBar->hide();
addLog("隐藏 工具栏");
}
}
void MainFramework::s_showPorjectWin() {
if (m_projectAc->isChecked()) {
m_projManagerView->show();
addLog("显示 项目区");
}
else {
m_projManagerView->hide();
addLog("隐藏 项目区");
}
}
void MainFramework::s_showPropertyWin() {
if (m_propertyAc->isChecked()) {
m_propertyView->show();
addLog("显示 属性区");
}
else {
m_propertyView->hide();
addLog("隐藏 属性区");
}
}
void MainFramework::s_showLogWin() {
if (m_logAc->isChecked()) {
m_logView->show();
addLog("显示 日志区");
}
else {
m_logView->hide();
addLog("隐藏 日志区");
}
}
void MainFramework::s_timeout() {
this->showMaximized();
}
void MainFramework::s_close() {
this->close();
}
void MainFramework::s_min() {
this->showMinimized();
}
void MainFramework::addLog(const QString& log)
{
m_logBody->setReadOnly(true);
m_logBody->append(log);
}
void MainFramework::initLogView() {
if (NULL == m_logView) {
m_logView = new QDockWidget(this);
//set dock widget feature: not move, enable close.
m_logView->setFeatures(QDockWidget::DockWidgetClosable);
m_logView->setWindowTitle("输出");
m_logBody = new QTextEdit(this);
m_logView->setWidget(m_logBody);
}
QPalette pl = m_logBody->palette();
pl.setBrush(QPalette::Base, QBrush(QColor(255, 0, 0, 0)));
m_logBody->setPalette(pl);
}
void MainFramework::initPropertyView() {
if (NULL == m_propertyView) {
m_propertyView = new QDockWidget(this);
m_propertyView->setFeatures(QDockWidget::DockWidgetClosable);
m_propertyView->setWindowTitle("飞行参数");
m_propertyWin = new FlyPropertyWin(this);
m_propertyView->setWidget(m_propertyWin);
qDebug() << "属性区 id:" << m_propertyView->winId() << ", name:" << m_propertyView->windowTitle();
}
}
void MainFramework::initProjectView() {
if (NULL == m_projManagerView) {
m_projManagerView = new QDockWidget(this);
m_projManagerView->setFeatures(QDockWidget::DockWidgetClosable);
m_projManagerView->setWindowTitle("图像列表");
delete title bar
//QWidget* lTitleBar = m_projManagerView->titleBarWidget();
//QWidget* lEmptyWidget = new QWidget();
//m_projManagerView->setTitleBarWidget(lEmptyWidget);
//delete lTitleBar;
m_projectWin = new ProjectWin(this);
m_projManagerView->setWidget(m_projectWin);
qDebug() << "项目区 id:" << m_projManagerView->winId() << ", name:" << m_projManagerView->windowTitle();
}
}
void MainFramework::dockLayout() {
this->addDockWidget(Qt::LeftDockWidgetArea, m_projManagerView, Qt::Orientation::Vertical);
this->addDockWidget(Qt::RightDockWidgetArea, m_propertyView, Qt::Orientation::Vertical);
this->addDockWidget(Qt::BottomDockWidgetArea, m_logView, Qt::Orientation::Vertical);
}
void MainFramework::initToolBar() {
QSize toolIconSize(50, 30);
ui->mainToolBar->setIconSize(toolIconSize); //设置工具栏图标大小
QIcon newFileIcon(":/images/新建文件.png");
QIcon openFileIcon(":/images/打开文件.png");
QIcon saveFileIcon(":/images/保存.png");
QIcon runIcon(":/images/运行.png");
QIcon stopIcon(":/images/停止.png");
QIcon debugIcon(":/images/调试.png");
QIcon anaysisIcon(":/images/分析.png");
QIcon recordIcon(":/images/录制.png");
QIcon grabIcon(":/images/抓取.png");
QIcon capacityIcon(":/images/应用中心.png");
QIcon paramterIcon(":/images/参数.png");
QIcon publishIcon(":/images/发布.png");
QIcon exportIcon(":/images/导出.png");
m_newAc = new QAction(newFileIcon, "新建项目", this);
m_saveAc = new QAction(saveFileIcon, "保存", this);
m_runAc = new QAction(runIcon, "运行", this);
m_stopAc = new QAction(stopIcon, "停止", this);
m_debugAc = new QAction(debugIcon, "调试", this);
m_anaysisAc = new QAction(anaysisIcon, "分析", this);
m_recordAc = new QAction(recordIcon, "录制", this);
m_grabAc = new QAction(grabIcon, "抓取", this);
m_capacityAc = new QAction(capacityIcon, "能力中心", this);
m_paramterAc = new QAction(paramterIcon, "参数", this);
m_publishAc = new QAction(publishIcon, "发布", this);
m_exportAc = new QAction(exportIcon, "导出", this);
//add QAction to Widget.
ui->mainToolBar->addAction(m_newAc);
ui->mainToolBar->addAction(m_saveAc);
ui->mainToolBar->addAction(m_saveAc);
ui->mainToolBar->addSeparator();
ui->mainToolBar->addAction(m_runAc);
ui->mainToolBar->addAction(m_stopAc);
ui->mainToolBar->addAction(m_debugAc);
ui->mainToolBar->addAction(m_anaysisAc);
ui->mainToolBar->addSeparator();
ui->mainToolBar->addAction(m_recordAc);
ui->mainToolBar->addAction(m_grabAc);
ui->mainToolBar->addSeparator();
ui->mainToolBar->addAction(m_capacityAc);
ui->mainToolBar->addSeparator();
ui->mainToolBar->addAction(m_paramterAc);
ui->mainToolBar->addAction(m_publishAc);
ui->mainToolBar->addAction(m_exportAc);
ui->mainToolBar->addSeparator();
connect(m_newAc, &QAction::triggered, this, &MainFramework::s_newProject);
connect(m_saveAc, &QAction::triggered, this, &MainFramework::s_saveFile);
connect(m_runAc, &QAction::triggered, this, &MainFramework::s_runAc);
connect(m_stopAc, &QAction::triggered, this, &MainFramework::s_stopAc);
connect(m_debugAc, &QAction::triggered, this, &MainFramework::s_debugAc);
connect(m_anaysisAc, &QAction::triggered, this, &MainFramework::s_anaysisAc);
connect(m_recordAc, &QAction::triggered, this, &MainFramework::s_recordAc);
connect(m_grabAc, &QAction::triggered, this, &MainFramework::s_grabAc);
connect(m_capacityAc, &QAction::triggered, this, &MainFramework::s_capacityAc);
connect(m_paramterAc, &QAction::triggered, this, &MainFramework::s_paramterAc);
connect(m_publishAc, &QAction::triggered, this, &MainFramework::s_publishAc);
connect(m_exportAc, &QAction::triggered, this, &MainFramework::s_exportAc);
}
void MainFramework::s_hideProj() {
m_projectAc->setChecked(false);
m_projManagerView->hide();
}
void MainFramework::s_propertyWinHide(bool visible) {
if (!visible) {
m_propertyAc->setChecked(false);
m_propertyView->hide();
}
}
void MainFramework::s_logWinHide(bool visible) {
if (!visible) {
m_logAc->setChecked(false);
m_logView->hide();
}
}
void MainFramework::s_newProject() {
}
void MainFramework::s_saveFile() {
QString log = QString("功能正在开发中,请耐心等待!");
showMessageBox(log);
addLog(log);
}
void MainFramework::s_runAc() {
QString log = QString("功能正在开发中,请耐心等待!");
showMessageBox(log);
addLog(log);
}
void MainFramework::s_stopAc() {
QString log = QString("功能正在开发中,请耐心等待!");
showMessageBox(log);
addLog(log);
}
void MainFramework::s_debugAc() {
QString log = QString("功能正在开发中,请耐心等待!");
showMessageBox(log);
addLog(log);
}
void MainFramework::s_anaysisAc() {
QString log = QString("功能正在开发中,请耐心等待!");
showMessageBox(log);
addLog(log);
}
void MainFramework::s_recordAc() {
QString log = QString("功能正在开发中,请耐心等待!");
showMessageBox(log);
addLog(log);
}
void MainFramework::s_grabAc() {
QString log = QString("功能正在开发中,请耐心等待!");
showMessageBox(log);
addLog(log);
}
void MainFramework::s_capacityAc() {
QString log = QString("功能正在开发中,请耐心等待!");
showMessageBox(log);
addLog(log);
}
void MainFramework::s_paramterAc() {
QString log = QString("功能正在开发中,请耐心等待!");
showMessageBox(log);
addLog(log);
}
void MainFramework::s_publishAc() {
QString log = QString("功能正在开发中,请耐心等待!");
showMessageBox(log);
addLog(log);
}
void MainFramework::s_exportAc() {
QString log = QString("功能正在开发中,请耐心等待!");
showMessageBox(log);
addLog(log);
}
void MainFramework::showMessageBox(const QString& text) {
if (NULL == m_msgBox) {
m_msgBox = new MsgBox();
}
m_msgBox->setMsgText(text);
m_msgBox->show();
}
MainFramework::~MainFramework()
{
delete ui;
}
上图是运行效果图,左边的树形视图少了一道分割竖线,看起来很纠结,解决办法:在左边的QDockWidget界面设计器中加入:
0
0
3
1
1
-
QTabWidget:pane {border-left:1px solid #a0a0a0; border-right:1px solid #a0a0a0;}
QTabBar::tab{min-height: 28px; min-width:80px ; font:14px; }
QTabBar::tab:!selected{}
QTabBar::tab:!selected:hover{ background-color: #d9fffe; color: black;}
"tabShape">
QTabWidget::Triangular
"currentIndex">
-1
"6" margin="11"/>