写了一些简单的示例程序,帮助我更好地使用和了解Qt-Advanced-Docking-System
1.写一个如图页面布局的程序
m_pDockManger=new ads::CDockManager(this);this->setCentralWidget(m_pDockManger);ads::CDockWidget* centerDockWidget=new ads::CDockWidget("中心页面");m_pDockManger->setCentralWidget(centerDockWidget);m_pDockManger->addDockWidget(ads::CenterDockWidgetArea,centerDockWidget);ads::CDockWidget* pDockWidget1=new ads::CDockWidget("页面1");m_pDockManger->addDockWidget(ads::LeftDockWidgetArea,pDockWidget1);ads::CDockWidget* pDockWidget2=new ads::CDockWidget("页面2");m_pDockManger->addDockWidget(ads::TopDockWidgetArea,pDockWidget2,pDockWidget1->dockAreaWidget());ads::CDockWidget* pDockWidget3=new ads::CDockWidget("页面3");m_pDockManger->addDockWidget(ads::RightDockWidgetArea,pDockWidget3,centerDockWidget->dockAreaWidget());ads::CDockWidget* pDockWidget4=new ads::CDockWidget("页面4");m_pDockManger->addDockWidget(ads::BottomDockWidgetArea,pDockWidget4,centerDockWidget->dockAreaWidget());
2.在centerDockWidget->dockAreaWidget()的ads::BottomDockWidgetArea添加widget
m_pDockManger=new ads::CDockManager(this);this->setCentralWidget(m_pDockManger);ads::CDockWidget* centerDockWidget=new ads::CDockWidget("中心页面");m_pDockManger->setCentralWidget(centerDockWidget);m_pDockManger->addDockWidget(ads::CenterDockWidgetArea,centerDockWidget);ads::CDockWidget* pDockWidget1=new ads::CDockWidget("页面1");m_pDockManger->addDockWidget(ads::LeftDockWidgetArea,pDockWidget1);ads::CDockWidget* pDockWidget2=new ads::CDockWidget("页面2");m_pDockManger->addDockWidget(ads::TopDockWidgetArea,pDockWidget2,pDockWidget1->dockAreaWidget());ads::CDockWidget* pDockWidget3=new ads::CDockWidget("页面3");m_pDockManger->addDockWidget(ads::RightDockWidgetArea,pDockWidget3,centerDockWidget->dockAreaWidget());ads::CDockWidget* pDockWidget4=new ads::CDockWidget("页面4");m_pDockManger->addDockWidget(ads::BottomDockWidgetArea,pDockWidget4,centerDockWidget->dockAreaWidget());ads::CDockWidget* pDockWidget5=new ads::CDockWidget("页面5");m_pDockManger->addDockWidget(ads::BottomDockWidgetArea,pDockWidget5,centerDockWidget->dockAreaWidget());ads::CDockWidget* pDockWidget6=new ads::CDockWidget("页面6");m_pDockManger->addDockWidget(ads::BottomDockWidgetArea,pDockWidget6,centerDockWidget->dockAreaWidget());
3.写一个如图所示的程序
m_pDockManger=new ads::CDockManager(this);this->setCentralWidget(m_pDockManger);ads::CDockWidget* centerDockWidget=new ads::CDockWidget("中心页面");centerDockWidget->setFeature(ads::CDockWidget::NoTab,true);m_pDockManger->setCentralWidget(centerDockWidget);m_pDockManger->addDockWidget(ads::CenterDockWidgetArea,centerDockWidget);ads::CDockWidget* pDockWidget1=new ads::CDockWidget("页面1");m_pDockManger->addDockWidget(ads::LeftDockWidgetArea,pDockWidget1);ads::CDockWidget* pDockWidget2=new ads::CDockWidget("页面2");m_pDockManger->addDockWidget(ads::TopDockWidgetArea,pDockWidget2,pDockWidget1->dockAreaWidget());ads::CDockWidget* pDockWidget3=new ads::CDockWidget("页面3");m_pDockManger->addDockWidget(ads::RightDockWidgetArea,pDockWidget3,centerDockWidget->dockAreaWidget());
4.
m_pDockManger=new ads::CDockManager(this);this->setCentralWidget(m_pDockManger);ads::CDockWidget* centerDockWidget=new ads::CDockWidget("中心页面");centerDockWidget->setFeature(ads::CDockWidget::NoTab,true);m_pDockManger->setCentralWidget(centerDockWidget);m_pDockManger->addDockWidget(ads::CenterDockWidgetArea,centerDockWidget);ads::CDockWidget* pDockWidget1=new ads::CDockWidget("页面1");m_pDockManger->addDockWidget(ads::LeftDockWidgetArea,pDockWidget1);ads::CDockWidget* pDockWidget2=new ads::CDockWidget("页面2");m_pDockManger->addDockWidget(ads::TopDockWidgetArea,pDockWidget2,pDockWidget1->dockAreaWidget());ads::CDockWidget* pDockWidget3=new ads::CDockWidget("页面3");m_pDockManger->addDockWidget(ads::RightDockWidgetArea,pDockWidget3,centerDockWidget->dockAreaWidget());ads::CDockWidget* pDockWidget4=new ads::CDockWidget("页面4");m_pDockManger->addDockWidget(ads::CenterDockWidgetArea,pDockWidget4,centerDockWidget->dockAreaWidget());ads::CDockWidget* pDockWidget5=new ads::CDockWidget("页面5");m_pDockManger->addDockWidget(ads::CenterDockWidgetArea,pDockWidget5,centerDockWidget->dockAreaWidget());ads::CDockWidget* pDockWidget6=new ads::CDockWidget("页面6");m_pDockManger->addDockWidget(ads::CenterDockWidgetArea,pDockWidget6,centerDockWidget->dockAreaWidget());
5.
m_pDockManger=new ads::CDockManager(this);this->setCentralWidget(m_pDockManger);ads::CDockWidget* centerDockWidget=new ads::CDockWidget("中心页面");centerDockWidget->setFeature(ads::CDockWidget::NoTab,true);m_pDockManger->setCentralWidget(centerDockWidget);m_pDockManger->addDockWidget(ads::CenterDockWidgetArea,centerDockWidget);ads::CDockWidget* pDockWidget1=new ads::CDockWidget("页面1");m_pDockManger->addDockWidget(ads::LeftDockWidgetArea,pDockWidget1);ads::CDockWidget* pDockWidget2=new ads::CDockWidget("页面2");m_pDockManger->addDockWidget(ads::TopDockWidgetArea,pDockWidget2,pDockWidget1->dockAreaWidget());ads::CDockWidget* pDockWidget3=new ads::CDockWidget("页面3");m_pDockManger->addDockWidget(ads::RightDockWidgetArea,pDockWidget3,centerDockWidget->dockAreaWidget());ads::CDockWidget* pDockWidget4=new ads::CDockWidget("页面4");m_pDockManger->addDockWidget(ads::CenterDockWidgetArea,pDockWidget4,centerDockWidget->dockAreaWidget());ads::CDockWidget* pDockWidget5=new ads::CDockWidget("页面5");m_pDockManger->addDockWidget(ads::CenterDockWidgetArea,pDockWidget5,centerDockWidget->dockAreaWidget());ads::CDockWidget* pDockWidget6=new ads::CDockWidget("页面6");m_pDockManger->addDockWidget(ads::CenterDockWidgetArea,pDockWidget6,centerDockWidget->dockAreaWidget());ads::CDockWidget* pDockWidget7=new ads::CDockWidget("页面7");m_pDockManger->addDockWidget(ads::BottomDockWidgetArea,pDockWidget7,centerDockWidget->dockAreaWidget());ads::CDockWidget* pDockWidget8=new ads::CDockWidget("页面8");m_pDockManger->addDockWidget(ads::CenterDockWidgetArea,pDockWidget8,pDockWidget7->dockAreaWidget());ads::CDockWidget* pDockWidget9=new ads::CDockWidget("页面9");m_pDockManger->addDockWidget(ads::CenterDockWidgetArea,pDockWidget9,pDockWidget7->dockAreaWidget());
6.
#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>
#include <QListWidget>
#include <QLabel>
#include "DockManager.h"
#include "QtDock/DockAreaWidget.h"
namespace Ui {
class MainWindow;
}class MainWindow : public QMainWindow
{Q_OBJECTpublic:explicit MainWindow(QWidget *parent = 0);~MainWindow();private slots:void on_actionAdd_triggered();private:Ui::MainWindow *ui;ads::CDockManager* m_pDockManger;ads::CDockWidget* centerDockWidget;ads::CDockWidget* bottomDockWidget;QListWidget* ListWidget;int mProjectNum=0;
};#endif // MAINWINDOW_H
#include "mainwindow.h"
#include "ui_mainwindow.h"MainWindow::MainWindow(QWidget *parent) :QMainWindow(parent),ui(new Ui::MainWindow)
{ui->setupUi(this);m_pDockManger=new ads::CDockManager(this);this->setCentralWidget(m_pDockManger);centerDockWidget=new ads::CDockWidget("中心页面");centerDockWidget->setFeature(ads::CDockWidget::NoTab,true);m_pDockManger->setCentralWidget(centerDockWidget);m_pDockManger->addDockWidget(ads::CenterDockWidgetArea,centerDockWidget);ads::CDockWidget* pDockWidget1=new ads::CDockWidget("测试页面1");m_pDockManger->addDockWidget(ads::LeftDockWidgetArea,pDockWidget1);ads::CDockWidget* pDockWidget2=new ads::CDockWidget("项目管理");m_pDockManger->addDockWidget(ads::TopDockWidgetArea,pDockWidget2,pDockWidget1->dockAreaWidget());ListWidget=new QListWidget(pDockWidget2);pDockWidget2->setWidget(ListWidget);ads::CDockWidget* pDockWidget3=new ads::CDockWidget("测试页面2");m_pDockManger->addDockWidget(ads::RightDockWidgetArea,pDockWidget3,centerDockWidget->dockAreaWidget());bottomDockWidget=new ads::CDockWidget("底部页面");bottomDockWidget->setFeature(ads::CDockWidget::NoTab,true);m_pDockManger->addDockWidget(ads::BottomDockWidgetArea,bottomDockWidget,centerDockWidget->dockAreaWidget());
}MainWindow::~MainWindow()
{delete ui;
}void MainWindow::on_actionAdd_triggered()
{mProjectNum++;ListWidget->addItem(QString("项目%1").arg(mProjectNum));ads::CDockWidget* cDockWidget=new ads::CDockWidget(QString("中心页面%1").arg(mProjectNum));m_pDockManger->addDockWidget(ads::CenterDockWidgetArea,cDockWidget,centerDockWidget->dockAreaWidget());ads::CDockWidget* bDockWidget=new ads::CDockWidget(QString("底部页面%1").arg(mProjectNum));m_pDockManger->addDockWidget(ads::CenterDockWidgetArea,bDockWidget,bottomDockWidget->dockAreaWidget());
}
7.dockWidgetsMap()
QMap<QString, ads::CDockWidget*> map=m_pDockManger->dockWidgetsMap();qDebug()<<map.keys();for(auto key:map.keys()){qDebug()<<key.toStdString().c_str();}
中心页面
中心页面1
中心页面2
中心页面3
中心页面4
底部页面
底部页面1
底部页面2
底部页面3
底部页面4
测试页面1
测试页面2
项目管理
qDebug()<<"centralWidget():"<<m_pDockManger->centralWidget()->objectName().toStdString().c_str();
centralWidget(): 中心页面
8.
QMap<QString, ads::CDockWidget*> map=m_pDockManger->dockWidgetsMap();for(auto key:map.keys()){qDebug()<<key.toStdString().c_str();auto value = map[key];qDebug()<<"ads::CDockWidget:"<<value;qDebug()<<"dockAreaWidget:"<<value->dockAreaWidget();}
中心页面
ads::CDockWidget: ads::CDockWidget(0x3052108, name = "\u4E2D\u5FC3\u9875\u9762")
dockAreaWidget: ads::CDockAreaWidget(0x30818f0)
中心页面1
ads::CDockWidget: ads::CDockWidget(0x41f25a8, name = "\u4E2D\u5FC3\u9875\u97621")
dockAreaWidget: ads::CDockAreaWidget(0x30818f0)
底部页面
ads::CDockWidget: ads::CDockWidget(0x30f8f68, name = "\u5E95\u90E8\u9875\u9762")
dockAreaWidget: ads::CDockAreaWidget(0x30f90a8)
底部页面1
ads::CDockWidget: ads::CDockWidget(0x41f26c0, name = "\u5E95\u90E8\u9875\u97621")
dockAreaWidget: ads::CDockAreaWidget(0x30f90a8)
测试页面1
ads::CDockWidget: ads::CDockWidget(0x3092060, name = "\u6D4B\u8BD5\u9875\u97621")
dockAreaWidget: ads::CDockAreaWidget(0x3091ea8)
测试页面2
ads::CDockWidget: ads::CDockWidget(0x30eca80, name = "\u6D4B\u8BD5\u9875\u97622")
dockAreaWidget: ads::CDockAreaWidget(0x30ecad0)
项目管理
ads::CDockWidget: ads::CDockWidget(0x30d81b0, name = "\u9879\u76EE\u7BA1\u7406")
dockAreaWidget: ads::CDockAreaWidget(0x30d84a8)
9.
QList<ads::CDockAreaWidget*> areas=m_pDockManger->openedDockAreas();qDebug()<<areas;
(ads::CDockAreaWidget(0x3071828), ads::CDockAreaWidget(0x3082448), ads::CDockAreaWidget(0x30c81b0), ads::CDockAreaWidget(0x30dd250), ads::CDockAreaWidget(0x30e8fb8))
对应5块区域
10.
QMap<QString, ads::CDockWidget*> map=m_pDockManger->dockWidgetsMap();for(auto key:map.keys()){qDebug()<<key.toStdString().c_str();auto value = map[key];qDebug()<<"ads::CDockWidget:"<<value;qDebug()<<"dockAreaWidget:"<<value->dockAreaWidget();}
area: ads::CDockAreaWidget(0x31118a0) count: 5
area: ads::CDockAreaWidget(0x3122128) count: 1
area: ads::CDockAreaWidget(0x3168458) count: 1
area: ads::CDockAreaWidget(0x317c440) count: 1
area: ads::CDockAreaWidget(0x3189440) count: 5
相关函数:
QList<CDockWidget*> CDockAreaWidget::dockWidgets() const
这个信号也比较好用
ads::CDockAreaWidget::currentChanged
11.
#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>
#include <QListWidget>
#include <QLabel>
#include "DockManager.h"
#include "QtDock/DockAreaWidget.h"
namespace Ui {
class MainWindow;
}class MainWindow : public QMainWindow
{Q_OBJECTpublic:explicit MainWindow(QWidget *parent = 0);~MainWindow();private slots:void on_actionAdd_triggered();private:Ui::MainWindow *ui;ads::CDockManager* m_pDockManger;ads::CDockWidget* centerDockWidget;QHash<int,ads::CDockWidget*> centerHash;QHash<int,ads::CDockWidget*> bottomHash;ads::CDockWidget* bottomDockWidget;QListWidget* ListWidget;int mProjectNum=0;QLabel * mLabelOne;QLabel * mLabelTwo;
};#endif // MAINWINDOW_H
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>MainWindow::MainWindow(QWidget *parent) :QMainWindow(parent),ui(new Ui::MainWindow)
{ui->setupUi(this);m_pDockManger=new ads::CDockManager(this);this->setCentralWidget(m_pDockManger);centerDockWidget=new ads::CDockWidget("中心页面");centerDockWidget->setFeature(ads::CDockWidget::NoTab,true);m_pDockManger->setCentralWidget(centerDockWidget);m_pDockManger->addDockWidget(ads::CenterDockWidgetArea,centerDockWidget);ads::CDockWidget* pDockWidget1=new ads::CDockWidget("测试页面1");m_pDockManger->addDockWidget(ads::LeftDockWidgetArea,pDockWidget1);mLabelOne=new QLabel(pDockWidget1);pDockWidget1->setWidget(mLabelOne);ads::CDockWidget* pDockWidget2=new ads::CDockWidget("项目管理");m_pDockManger->addDockWidget(ads::TopDockWidgetArea,pDockWidget2,pDockWidget1->dockAreaWidget());ListWidget=new QListWidget(pDockWidget2);connect(ListWidget,&QListWidget::currentItemChanged,this,[=](QListWidgetItem *current, QListWidgetItem *previous){int index=current->data(Qt::UserRole).toInt();ads::CDockAreaWidget* centerArea=centerDockWidget->dockAreaWidget();centerArea->setCurrentDockWidget(centerHash[index]);ads::CDockAreaWidget* bottomArea=bottomDockWidget->dockAreaWidget();bottomArea->setCurrentDockWidget(bottomHash[index]);QString msg=QString("当时是项目%1").arg(index);mLabelOne->setText(msg);mLabelTwo->setText(msg);});pDockWidget2->setWidget(ListWidget);ads::CDockWidget* pDockWidget3=new ads::CDockWidget("测试页面2");m_pDockManger->addDockWidget(ads::RightDockWidgetArea,pDockWidget3,centerDockWidget->dockAreaWidget());mLabelTwo=new QLabel(pDockWidget3);pDockWidget3->setWidget(mLabelTwo);bottomDockWidget=new ads::CDockWidget("底部页面");bottomDockWidget->setFeature(ads::CDockWidget::NoTab,true);m_pDockManger->addDockWidget(ads::BottomDockWidgetArea,bottomDockWidget,centerDockWidget->dockAreaWidget());
}MainWindow::~MainWindow()
{delete ui;
}void MainWindow::on_actionAdd_triggered()
{mProjectNum++;QListWidgetItem *item;item=new QListWidgetItem(ListWidget);item->setText(QString("项目%1").arg(mProjectNum));item->setData(Qt::UserRole,mProjectNum);ListWidget->addItem(item);ListWidget->setCurrentItem(item);ads::CDockWidget* cDockWidget=new ads::CDockWidget(QString("中心页面%1").arg(mProjectNum));m_pDockManger->addDockWidget(ads::CenterDockWidgetArea,cDockWidget,centerDockWidget->dockAreaWidget());centerHash.insert(mProjectNum,cDockWidget);ads::CDockWidget* bDockWidget=new ads::CDockWidget(QString("底部页面%1").arg(mProjectNum));m_pDockManger->addDockWidget(ads::CenterDockWidgetArea,bDockWidget,bottomDockWidget->dockAreaWidget());bottomHash.insert(mProjectNum,bDockWidget);QString msg=QString("当时是项目%1").arg(mProjectNum);mLabelOne->setText(msg);mLabelTwo->setText(msg);
}
12.
#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>
#include <QListWidget>
#include <QLabel>
#include "DockManager.h"
#include "QtDock/DockAreaWidget.h"
namespace Ui {
class MainWindow;
}class MainWindow : public QMainWindow
{Q_OBJECTpublic:explicit MainWindow(QWidget *parent = 0);~MainWindow();private slots:void on_actionAdd_triggered();void onProjectChanged(int index);
private:Ui::MainWindow *ui;ads::CDockManager* m_pDockManger;ads::CDockWidget* centerDockWidget;ads::CDockWidget* bottomDockWidget;QListWidget* ListWidget;QHash<int,ads::CDockWidget*> centerHash;QHash<int,ads::CDockWidget*> bottomHash;QHash<int,QListWidgetItem*> itemHash;int mProjectNum=0;QLabel * mLabelOne;QLabel * mLabelTwo;
};#endif // MAINWINDOW_H
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>MainWindow::MainWindow(QWidget *parent) :QMainWindow(parent),ui(new Ui::MainWindow)
{ui->setupUi(this);m_pDockManger=new ads::CDockManager(this);qDebug()<<"m_pDockManger:"<<m_pDockManger;this->setCentralWidget(m_pDockManger);centerDockWidget=new ads::CDockWidget("中心页面");centerDockWidget->setFeature(ads::CDockWidget::NoTab,true);m_pDockManger->setCentralWidget(centerDockWidget);m_pDockManger->addDockWidget(ads::CenterDockWidgetArea,centerDockWidget);ads::CDockAreaWidget* centerArea=centerDockWidget->dockAreaWidget();connect(centerArea,&ads::CDockAreaWidget::currentChanged,this,&MainWindow::onProjectChanged);bottomDockWidget=new ads::CDockWidget("底部页面");bottomDockWidget->setFeature(ads::CDockWidget::NoTab,true);m_pDockManger->addDockWidget(ads::BottomDockWidgetArea,bottomDockWidget,centerDockWidget->dockAreaWidget());ads::CDockAreaWidget* bottomArea=bottomDockWidget->dockAreaWidget();connect(bottomArea,&ads::CDockAreaWidget::currentChanged,this,&MainWindow::onProjectChanged);ads::CDockWidget* pDockWidget1=new ads::CDockWidget("测试页面1");m_pDockManger->addDockWidget(ads::LeftDockWidgetArea,pDockWidget1);mLabelOne=new QLabel(pDockWidget1);pDockWidget1->setWidget(mLabelOne);ads::CDockWidget* pDockWidget2=new ads::CDockWidget("项目管理");m_pDockManger->addDockWidget(ads::TopDockWidgetArea,pDockWidget2,pDockWidget1->dockAreaWidget());ListWidget=new QListWidget(pDockWidget2);pDockWidget2->setWidget(ListWidget);connect(ListWidget,&QListWidget::currentItemChanged,this,[=](QListWidgetItem *current, QListWidgetItem *previous){int index=current->data(Qt::UserRole).toInt();onProjectChanged(index);});ads::CDockWidget* pDockWidget3=new ads::CDockWidget("测试页面2");m_pDockManger->addDockWidget(ads::RightDockWidgetArea,pDockWidget3,centerDockWidget->dockAreaWidget());mLabelTwo=new QLabel(pDockWidget3);pDockWidget3->setWidget(mLabelTwo);
}MainWindow::~MainWindow()
{delete ui;
}void MainWindow::on_actionAdd_triggered()
{mProjectNum++;QListWidgetItem *item;item=new QListWidgetItem(ListWidget);item->setText(QString("项目%1").arg(mProjectNum));item->setData(Qt::UserRole,mProjectNum);ListWidget->addItem(item);itemHash.insert(mProjectNum,item);ListWidget->setCurrentItem(item);ads::CDockWidget* cDockWidget=new ads::CDockWidget(QString("中心页面%1").arg(mProjectNum));m_pDockManger->addDockWidget(ads::CenterDockWidgetArea,cDockWidget,centerDockWidget->dockAreaWidget());centerHash.insert(mProjectNum,cDockWidget);ads::CDockWidget* bDockWidget=new ads::CDockWidget(QString("底部页面%1").arg(mProjectNum));m_pDockManger->addDockWidget(ads::CenterDockWidgetArea,bDockWidget,bottomDockWidget->dockAreaWidget());bottomHash.insert(mProjectNum,bDockWidget);QString msg=QString("当时是项目%1").arg(mProjectNum);mLabelOne->setText(msg);mLabelTwo->setText(msg);
}void MainWindow::onProjectChanged(int index)
{ListWidget->setCurrentItem(itemHash[index]);ads::CDockAreaWidget* centerArea=centerDockWidget->dockAreaWidget();centerArea->setCurrentDockWidget(centerHash[index]);ads::CDockAreaWidget* bottomArea=bottomDockWidget->dockAreaWidget();bottomArea->setCurrentDockWidget(bottomHash[index]);QString msg=QString("当时是项目%1").arg(index);mLabelOne->setText(msg);mLabelTwo->setText(msg);
}