bool CSettings::saveSplitterSizes(QSplitter * split, const QString & key, const QString & prefix) { QList<int> sizes = split->sizes(); QString data; for(int i=0; i<sizes.size(); i++) { if(i>0) data += "*"; data += QString::number( sizes.at(i) ); }//for if(prefix.isEmpty()) setValue(key, data); else setValue(prefix + "/" + key, data); return true;}bool CSettings::setSplitterSizes(QSplitter * split, const QString & key, const QString & prefix) { QString data; if(prefix.isEmpty()) data = key; else data = prefix + "/" + key; data = value(data, QVariant()).toString(); if(data.isEmpty()) return false; QStringList list = data.split(QChar('*'), QString::SkipEmptyParts); QList<int> sizes; bool ok = false; for(int i=0; i<list.size(); i++) { sizes << list.at(i).toInt(&ok); if(!ok) return false; }//for split->setSizes( sizes ); return true;}bool CSettings::saveGuiStateWindow(QWidget * wnd, const QString & key, const QString & prefix) { QString path; if(prefix.isEmpty()) path = key; else path = prefix +"/"+ key; setValue(path + "_state", (int)wnd->windowState()); if(!(wnd->windowState() & Qt::WindowMaximized)) setValue(path + "_geometry", wnd->geometry()); return true;}bool CSettings::setGuiStateWindow(QWidget * wnd, const QString & key, const QString & prefix) { QString path; if(prefix.isEmpty()) path = key; else path = prefix +"/"+ key; QRect geometry = value(path + "_geometry", wnd->geometry()).toRect(); if(geometry.isValid()) wnd->setGeometry(geometry); int state = value(path + "_state", Qt::WindowMaximized).toInt(); if(state & Qt::WindowMaximized) wnd->setWindowState(Qt::WindowMaximized); else wnd->setWindowState(Qt::WindowNoState); return true;}
void MainWindow::saveLayout(){ QFile file(QCoreApplication::applicationDirPath()+"/settings.set"); if (!file.open(QFile::WriteOnly)) { QString msg = tr("Failed to open %1\n%2") .arg("settings.set") .arg(file.errorString()); QMessageBox::warning(this, tr("Error"), msg); return; } QByteArray geo_data = saveGeometry(); QByteArray layout_data = saveState(); bool ok = file.putChar((uchar)geo_data.size()); if (ok) ok = file.write(geo_data) == geo_data.size(); if (ok) ok = file.write(layout_data) == layout_data.size(); if (!ok) { QString msg = tr("Error writing to %1\n%2") .arg("settings.set") .arg(file.errorString()); QMessageBox::warning(this, tr("Error"), msg); return; }}void MainWindow::loadLayout(){ QFile file(QCoreApplication::applicationDirPath()+"/settings.set"); if (!file.open(QFile::ReadOnly)) { QString msg = tr("Failed to open %1\n%2") .arg("settings.set") .arg(file.errorString()); QMessageBox::warning(this, tr("Error"), msg); return; } uchar geo_size; QByteArray geo_data; QByteArray layout_data; bool ok = file.getChar((char*)&geo_size); if (ok) { geo_data = file.read(geo_size); ok = geo_data.size() == geo_size; } if (ok) { layout_data = file.readAll(); ok = layout_data.size() > 0; } if (ok) ok = restoreGeometry(geo_data); if (ok) ok = restoreState(layout_data); if (!ok) { QString msg = tr("Error reading %1") .arg("settings.set"); QMessageBox::warning(this, tr("Error"), msg); return; }}
bool restoreState ( const QByteArray & state )QByteArray saveState () const