How to save non-MainWindow size/position [Solved]
-
I am writing a desktop application that has a main window and two pop up windows (lets call those the Log and Browser).
I figured out how to save size/position of the main window using this code:
void MainWindow::writePositionSettings() { QSettings qsettings("esp", "lotus"); qsettings.beginGroup("mainwindow"); qsettings.setValue("geometry", saveGeometry()); qsettings.setValue("savestate", saveState()); qsettings.setValue( "maximized", isMaximized() ); if ( !isMaximized() ) { qsettings.setValue( "pos", pos() ); qsettings.setValue( "size", size() ); } qsettings.endGroup(); qDebug() << "Saving main window size and position."; } void MainWindow::readPositionSettings() { QSettings qsettings( "esp", "lotus" ); qsettings.beginGroup( "mainwindow" ); restoreGeometry(qsettings.value( "geometry", saveGeometry() ).toByteArray()); restoreState(qsettings.value( "savestate", saveState() ).toByteArray()); move(qsettings.value( "pos", pos() ).toPoint()); resize(qsettings.value( "size", size() ).toSize()); if ( qsettings.value( "maximized", isMaximized() ).toBool() ) showMaximized(); qsettings.endGroup(); }
This works with my main window. My original assumption was I could "daisy chain" the storage pattern by putting similar code in my Browser and Log window. but Qt complained it didn't recognize the commands. I wondered if QSettings was just for a main window and not auxiliary windows. Just hoping for Qt Guru to give me some pointers.
-
Sure you can use QSettings to save any Windows, Dialog geometry,
Just make sure you load the settings and use a different "group" so your settings do not clash togethers.
Here is a working example of one of my QDialog:
void WorkoutDialog::loadInterface() { QSettings settings("MaxPowerVelo", "PowerVelo"); settings.beginGroup("WorkoutDialog"); resize(settings.value("size", QSize(1024, 768)).toSize()); move(settings.value("pos", QPoint(200, 200)).toPoint()); QList<int> mySize; mySize.append( settings.value("splitter0", 2).toInt() ); mySize.append( settings.value("splitter1", 35).toInt() ); mySize.append( settings.value("splitter2", 37).toInt() ); mySize.append( settings.value("splitter3", 27).toInt() ); ui->splitter->setSizes(mySize); bool wasFullscreen = settings.value("fullscreen", false).toBool(); settings.endGroup(); if (wasFullscreen) { wasFullscreenOnOpen = true; showFullScreenWin(); } else { wasFullscreenOnOpen = false; this->setSizeGripEnabled(true); ui->widget_topMenu->setMinExpandExitVisible(false); ui->widget_topMenu->updateExpandIcon(); } } void WorkoutDialog::saveInterface() { //// ------------ Save interface QSettings settings("MaxPowerVelo", "PowerVelo"); settings.beginGroup("WorkoutDialog"); settings.setValue("size", size()); settings.setValue("pos", pos()); QList<int> sizeNow = ui->splitter->sizes(); settings.setValue("splitter0", sizeNow.at(0)); settings.setValue("splitter1", sizeNow.at(1)); settings.setValue("splitter2", sizeNow.at(2)); settings.setValue("splitter3", sizeNow.at(3)); if (this->isFullScreen()) { settings.setValue("fullscreen", true); } else { settings.setValue("fullscreen", false); } settings.endGroup(); }
-
I only had a few minutes this morning to really try this. I will give it another shot tonight.
You actually answered one of my other questions about how to save sub-components (like a splitter) in your pasted code. So you can stick a "I am awesome" gold star to your forehead for the day. ;)