Qt-Advanced-Docking-System开源项目,dock窗口完美布局
源码下载:
https://github.com/githubuser0xFFFF/Qt-Advanced-Docking-System
使用方法:
准备工作
1、解压源码 Qt-Advanced-Docking-System-master
,使用Qt Creator打开 ads.pro
文件,配置我选的是 Qt 5.12.12 MinGW 32-bit
。
2、构建项目,生成debug文件夹,打开debug文件夹=>lib,这里面是一些示例,我们单独使用需要使用的是 libqtadvanceddockingd.a
和 qtadvanceddockingd.dll
两个文件
3、将源码目录src文件夹
中的头文件提取出来单独放在一个文件夹中(注意这里不是构建生成的debug目录中的src文件夹 )
4、以上是一些准备工作,总结一下就是需要得到以下的文件。
示例使用
本示例使用的是
Qt-Advanced-Docking-System
中,examples文件夹=>emptydockarea项目示例
1、新建qt项目,项目名为 dockingTest
(项目名随心取)
2、将准备工作得到的 header文件夹
和 lib文件夹
复制到 dockingTest.pro
的同级目录下
3、在 dockingTest.pro
中添加以下语句
win32: LIBS += -L$$PWD/lib -lqtadvanceddockingd
INCLUDEPATH += $$PWD/header
DEPENDPATH += $$PWD/header
如图:
4、mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QComboBox>
#include <QWidgetAction>
#include "DockManager.h"
#include "DockAreaWidget.h"
#include "DockWidget.h"
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
protected:
virtual void closeEvent(QCloseEvent* event) override;
private:
Ui::MainWindow *ui;
QAction* SavePerspectiveAction = nullptr;
QWidgetAction* PerspectiveListAction = nullptr;
QComboBox* PerspectiveComboBox = nullptr;
ads::CDockManager* DockManager;
ads::CDockAreaWidget* StatusDockArea;
ads::CDockWidget* TimeLineDockWidget;
void createPerspectiveUi();
private slots:
void savePerspective();
};
#endif // MAINWINDOW_H
5、mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QWidgetAction>
#include <QLabel>
#include <QCalendarWidget>
#include <QTreeView>
#include <QFileSystemModel>
#include <QTableWidget>
#include <QHBoxLayout>
#include <QRadioButton>
#include <QPushButton>
#include <QInputDialog>
#include <QFileDialog>
#include <QSettings>
#include <QMessageBox>
#include <QPlainTextEdit>
#include <QToolBar>
#include "DockAreaWidget.h"
#include "DockAreaTitleBar.h"
#include "DockAreaTabBar.h"
#include "FloatingDockContainer.h"
#include "DockComponentsFactory.h"
#include "DockManager.h"
using namespace ads;
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
CDockManager::setConfigFlag(CDockManager::OpaqueSplitterResize,true);
CDockManager::setConfigFlag(CDockManager::XmlCompressionEnabled,false);
CDockManager::setConfigFlag(CDockManager::FocusHighlighting,true);
DockManager = new CDockManager(this);
QLabel* label = new QLabel();
label->setText("this is a dockarea which is always visible, even if it does not contain any dockwidgets.");
label->setAlignment(Qt::AlignCenter);
CDockWidget* CentralDockWidget = new CDockWidget("CentralWidget");
CentralDockWidget->setWidget(label);
CentralDockWidget->setFeature(ads::CDockWidget::NoTab,true);
auto* CentralDockArea = DockManager->setCentralWidget(CentralDockWidget);
QTableWidget* table = new QTableWidget();
table->setColumnCount(3);
table->setRowCount(10);
CDockWidget* TableDockWidget = new CDockWidget("Table 1");
TableDockWidget->setWidget(table);
TableDockWidget->setMinimumSizeHintMode(CDockWidget::MinimumSizeHintFromDockWidget);
TableDockWidget->resize(250,150);
TableDockWidget->setMinimumSize(200,150);
DockManager->addDockWidgetTabToArea(TableDockWidget,CentralDockArea);
auto TableArea = DockManager->addDockWidget(DockWidgetArea::LeftDockWidgetArea,TableDockWidget);
ui->menuView->addAction(TableDockWidget->toggleViewAction());
table = new QTableWidget();
table->setColumnCount(5);
table->setRowCount(200);
TableDockWidget = new CDockWidget("Table 2");
TableDockWidget->setWidget(table);
TableDockWidget->setMinimumSizeHintMode(CDockWidget::MinimumSizeHintFromDockWidget);
TableDockWidget->resize(250,150);
TableDockWidget->setMinimumSize(200,150);
DockManager->addDockWidget(DockWidgetArea::BottomDockWidgetArea,TableDockWidget,TableArea);
ui->menuView->addAction(TableDockWidget->toggleViewAction());
QTableWidget* propertiesTable = new QTableWidget();
propertiesTable->setColumnCount(50);
propertiesTable->setRowCount(100);
CDockWidget* PropertiesDockWidget = new CDockWidget("Properties");
PropertiesDockWidget->setWidget(propertiesTable);
PropertiesDockWidget->setMinimumSizeHintMode(CDockWidget::MinimumSizeHintFromDockWidget);
PropertiesDockWidget->resize(250,150);
PropertiesDockWidget->setMinimumSize(200,150);
DockManager->addDockWidget(DockWidgetArea::RightDockWidgetArea,PropertiesDockWidget,CentralDockArea);
ui->menuView->addAction(PropertiesDockWidget->toggleViewAction());
createPerspectiveUi();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::createPerspectiveUi(){
SavePerspectiveAction = new QAction("Create Perspective", this);
connect(SavePerspectiveAction,SIGNAL(triggered()),SLOT(savePerspective()));
PerspectiveListAction = new QWidgetAction(this);
PerspectiveComboBox = new QComboBox(this);
PerspectiveComboBox->setSizeAdjustPolicy(QComboBox::AdjustToContents);
PerspectiveComboBox->setSizePolicy(QSizePolicy::Preferred,QSizePolicy::Preferred);
connect(PerspectiveComboBox,SIGNAL(activated(const QString&)),DockManager,SLOT(openPerspective(const QString&)));
PerspectiveListAction->setDefaultWidget(PerspectiveComboBox);
ui->toolBar->addSeparator();
ui->toolBar->addAction(PerspectiveListAction);
ui->toolBar->addAction(SavePerspectiveAction);
}
void MainWindow::savePerspective(){
QString PerspectiveName = QInputDialog::getText(this,"save perspective", "enter unique name: ");
if(PerspectiveName.isEmpty()){
return;
}
DockManager->addPerspective(PerspectiveName);
QSignalBlocker Blocker(PerspectiveComboBox);
PerspectiveComboBox->clear();
PerspectiveComboBox->addItems(DockManager->perspectiveNames());
PerspectiveComboBox->setCurrentText(PerspectiveName);
}
void MainWindow::closeEvent(QCloseEvent* event)
{
DockManager->deleteLater();
QMainWindow::closeEvent(event);
}
6、mainwindow.ui
7、运行结果