0
点赞
收藏
分享

微信扫一扫

Qt保存和恢复页面布局


一、前言

很多软件在界面上的操作是很灵活的,例如PS,它的主面板是一个个吸附窗口堆叠起来的,用户可以自由拖拽、吸附,并且关闭软件再次打开软件,发现之前的拖拽、吸附完全保存下来了,所以用户可以自由的构建自己舒适的办公环境(软件界面),那这个效果是怎么实现的呢?

在Qt中使用​​saveState()​​​和​​restoreState(QByteArray)​​就可以完美的实现这种效果。

QByteArray QMainWindow::saveState(int version = 0) const;
bool QMainWindow::restoreState(const QByteArray &state, int version = 0);

二、效果展示

1、自由调整布局

Qt保存和恢复页面布局_开发语言


2、重新打开软件

Qt保存和恢复页面布局_qt_02


Qt保存和恢复页面布局_#include_03

三、关键步骤

在程序的出口处,使用saveState保存界面布局

QString strPath = QCoreApplication::applicationDirPath() + "/UILayout.ini";
QFile file(strPath);
if(file.open(QIODevice::WriteOnly)) {
QDataStream outfile(&file);
QByteArray ba = this->saveState();
outfile<<ba;
file.close();
}

在程序的入口处,使用restoreState恢复界面布局

QString strPath = QCoreApplication::applicationDirPath() + "/UILayout.ini";
QFile file(strPath);
if(file.open(QIODevice::ReadOnly)) {
QByteArray ba;
QDataStream in(&file);
in>>ba;
file.close();
this->restoreState(ba);
}

MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);

//程序入口处
QString strPath = QCoreApplication::applicationDirPath() + "/UILayout.ini";
QFile file(strPath);
if(file.open(QIODevice::ReadOnly)) {
QByteArray ba;
QDataStream in(&file);
in>>ba;
file.close();
this->restoreState(ba);
}
}

void MainWindow::closeEvent(QCloseEvent *event)
{
//程序出口处
QString strPath = QCoreApplication::applicationDirPath() + "/UILayout.ini";
QFile file(strPath);
if(file.open(QIODevice::WriteOnly))
{
QDataStream outfile(&file);
QByteArray ba = this->saveState();
outfile<<ba;
file.close();
}
}

四、详细代码

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QDebug>
#include <QCloseEvent>
#include <QDataStream>
#include <QByteArray>
#include <QCoreApplication>
#include <QMenuBar>
#include <QToolBar>
#include <QDockWidget>
#include <QLabel>

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();

void CreateMenuBar();
void CreateToolBar();
void CreateStatusBar();

protected:
void closeEvent(QCloseEvent *event);

private:
Ui::MainWindow *ui;

QDockWidget *dock_Image; // 图像窗口
QDockWidget* dock_Tool;// 工具箱窗口
QDockWidget* dock_Geom;// 几何变换窗口
QDockWidget* dock_Gray;// 灰度变换窗口
QDockWidget* dock_Enhance;// 图像增强窗口
QDockWidget* dock_Morp;// 形态学处理窗口
QDockWidget* dock_Color;// 颜色模型窗口
QDockWidget* dock_Prop;// 属性窗口
QDockWidget* dock_Output;// 输出窗口
};
#endif // MAINWINDOW_H

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);

QWidget* p = takeCentralWidget(); //删除中央窗体
if (p) {
delete p;
}

setDockNestingEnabled(true); //允许嵌套dock

//------------------------------------------------------------------------------
//创建菜单栏、工具栏、状态栏
CreateMenuBar();
CreateToolBar();
CreateStatusBar();

//------------------------------------------------------------------------------
//创建DockWidget
QDockWidget* dock_Image = new QDockWidget(tr("图像"), this);
dock_Image->setObjectName("dock_Image");
dock_Image->setFeatures(QDockWidget::DockWidgetMovable | QDockWidget::DockWidgetFloatable); // 设置为可移动可浮动,但不可关闭
dock_Image->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea); // 可移动范围:左右
dock_Image->setMinimumSize(600, 600); // 设置最小宽高

dock_Tool = new QDockWidget(tr("工具箱"), this); // 工具箱窗口,若想设置特征或移动范围,方法同上。
dock_Tool->setObjectName("dock_Tool");

dock_Geom = new QDockWidget(tr("几何变换"), this); // 几何变换窗口
dock_Geom->setObjectName("dock_Geom");

dock_Gray = new QDockWidget(tr("灰度变换"), this); // 灰度变换窗口
dock_Gray->setObjectName("dock_Gray");

dock_Enhance = new QDockWidget(tr("图像增强"), this); // 图像增强窗口
dock_Enhance->setObjectName("dock_Enhance");

dock_Morp = new QDockWidget(tr("形态学处理"), this); // 形态学处理窗口
dock_Morp->setObjectName("dock_Morp");

dock_Color = new QDockWidget(tr("颜色模型"), this); // 颜色模型窗口
dock_Color->setObjectName("dock_Color");

dock_Prop = new QDockWidget(tr("属性"), this); // 属性窗口
dock_Prop->setObjectName("dock_Prop");

dock_Output = new QDockWidget(tr("输出"), this); // 输出窗口
dock_Output->setObjectName("dock_Output");

// 进行布局
setCentralWidget(dock_Image); // 指定为中心窗口
addDockWidget(Qt::LeftDockWidgetArea, dock_Tool);
addDockWidget(Qt::BottomDockWidgetArea, dock_Output);
addDockWidget(Qt::RightDockWidgetArea, dock_Geom);
addDockWidget(Qt::RightDockWidgetArea, dock_Gray);
addDockWidget(Qt::RightDockWidgetArea, dock_Enhance);
addDockWidget(Qt::RightDockWidgetArea, dock_Morp);
addDockWidget(Qt::RightDockWidgetArea, dock_Color);
addDockWidget(Qt::RightDockWidgetArea, dock_Prop);

// 分割窗口
splitDockWidget(dock_Tool, dock_Image, Qt::Horizontal); // 水平
splitDockWidget(dock_Geom, dock_Output, Qt::Vertical); // 垂直

// 合并窗口
tabifyDockWidget(dock_Geom, dock_Gray);
tabifyDockWidget(dock_Gray, dock_Enhance);
tabifyDockWidget(dock_Enhance, dock_Morp);
tabifyDockWidget(dock_Morp, dock_Color);

tabifyDockWidget(dock_Output, dock_Prop);

dock_Geom->raise(); // raise()函数可使指定窗口置于最前

//-------------------------------------------------------------------------
//程序入口处
QString strPath = QCoreApplication::applicationDirPath() + "/UILayout.ini";
QFile file(strPath);
if(file.open(QIODevice::ReadOnly))
{
QByteArray ba;
QDataStream in(&file);
in>>ba;
file.close();
this->restoreState(ba);
}
}

MainWindow::~MainWindow()
{

}

void MainWindow::closeEvent(QCloseEvent *event)
{
//程序出口处
QString strPath = QCoreApplication::applicationDirPath() + "/UILayout.ini";
QFile file(strPath);
if(file.open(QIODevice::WriteOnly))
{
QDataStream outfile(&file);
QByteArray ba = this->saveState();
outfile<<ba;
file.close();
}
}

void MainWindow::CreateMenuBar()
{
QMenuBar * menuBar = new QMenuBar(this);

QMenu * menuFile = new QMenu("File", this);
QAction * newFile = new QAction(QIcon(), "NewFile", this);
QAction * openFile = new QAction(QIcon(), "OpenFile", this);
QAction * save = new QAction(QIcon(), "Save", this);
menuFile->addAction(newFile);
menuFile->addAction(openFile);
menuFile->addAction(save);

QMenu * menuEdit = new QMenu("Edit", this);
QAction * cut = new QAction(QIcon(), "Cut", this);
QAction * copy = new QAction(QIcon(), "Copy", this);
QAction * paste = new QAction(QIcon(), "Paste", this);
menuEdit->addAction(cut);
menuEdit->addAction(copy);
menuEdit->addAction(paste);

menuBar->addMenu(menuFile);
menuBar->addMenu(menuEdit);

this->setMenuBar(menuBar);
}

void MainWindow::CreateToolBar()
{
QToolBar * toolBar1 = new QToolBar(this);
toolBar1->setObjectName("toolBar1");
QAction * action1_1 = new QAction("action1_1", this);
QAction * action1_2 = new QAction("action1_2", this);
QAction * action1_3 = new QAction("action1_3", this);
QAction * action1_4 = new QAction("action1_4", this);
toolBar1->addAction(action1_1);
toolBar1->addAction(action1_2);
toolBar1->addAction(action1_3);
toolBar1->addAction(action1_4);

QToolBar * toolBar2 = new QToolBar(this);
toolBar2->setObjectName("toolBar2");
QAction * action2_1 = new QAction("action2_1", this);
QAction * action2_2 = new QAction("action2_2", this);
QAction * action2_3 = new QAction("action2_3", this);
QAction * action2_4 = new QAction("action2_4", this);
toolBar2->addAction(action2_1);
toolBar2->addAction(action2_2);
toolBar2->addAction(action2_3);
toolBar2->addAction(action2_4);

QToolBar * toolBar3 = new QToolBar(this);
toolBar3->setObjectName("toolBar3");
QAction * action3_1 = new QAction("action3_1", this);
QAction * action3_2 = new QAction("action3_2", this);
QAction * action3_3 = new QAction("action3_3", this);
QAction * action3_4 = new QAction("action3_4", this);
toolBar3->addAction(action3_1);
toolBar3->addAction(action3_2);
toolBar3->addAction(action3_3);
toolBar3->addAction(action3_4);

this->addToolBar(toolBar1);
this->addToolBar(toolBar2);
this->addToolBar(toolBar3);
}

void MainWindow::CreateStatusBar()
{
//创建状态栏
QStatusBar *status = new QStatusBar(this);
status->setObjectName("status");
status->setObjectName("状态栏");
status->setStyleSheet("QStatusBar::item{border: 0px}"); //设置不显示label的边框

//主窗口添加状态栏
this->setStatusBar(status);

//创建标签
QLabel *statusLabel = new QLabel("我是状态栏", this);

//状态栏添加信息
status->showMessage("我在3秒后会消失", 3000);//显示在左侧,并且3秒后自动消失

status->addPermanentWidget(statusLabel);//添加右侧标签(永久性)
}


举报

相关推荐

0 条评论