如何使用savestate()和restoreState()来保存和恢复工具条,状态栏,其它视图等的页面布局
运用场景:创建一个基于QMainWindow的QT Widgets Application,然后根据需要添加菜单栏、工具栏、状态栏等等;这是工具栏有很多,好多都叠在一起,经过拖拽达到预期效果,但是下次重新打开时,布局又乱了。所以现在需要解决的是如何保存拖拽后的布局效果
- 在程序的出口处,增加保存布局的操作,具体如下:
QString strPath = "usr/test/Myproject/MenuLayout.ini";
QFile file(strPath);
if(file.open(QIODevice::WriteOnly))
{
QDataStream outfile(&file);
QByteArray ba = m_pMainFram->saveState();
ba<<outfile;
file.close();
}
- 在程序的入口处,增加导入布局的操作,具体如下
QString strPath = "usr/test/Myproject/MenuLayout.ini";
QFile file(strPath);
if(file.open(QIODevice::ReadOnly))
{
QByteArray ba;
QDataStream in(&file);
in>>ba;
file.close();
m_pMainFrame->restoreState(ba);
}