Monterey version 3.0 now has an adjustable UI! Not only is the window itself able to be scaled, but now you can apportion the UI how you'd like. If you want, you can make the graphs taller or shorter (even hide them), and you can adjust the widths of both side panels. Upon closing, Monterey saves your window geometry so when you open it again, the UI is setup the way you like.
Here are the relevant pieces of code:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//mySettings is a QSettings pointer that saves to an INI file | |
mySettings->beginGroup("mainWindow"); | |
mySettings->setValue("geometry", saveGeometry()); | |
mySettings->setValue("splitterStateHorizontal", ui->splitterHorizontal->saveState()); | |
mySettings->setValue("splitterStateVertical", ui->splitterVertical->saveState()); | |
mySettings->endGroup(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
QFile settingsFile(mySettings->fileName()); | |
if(settingsFile.exists()) //if there are settings to load | |
{ | |
//Load the settings | |
mySettings->beginGroup("mainWindow"); | |
restoreGeometry(mySettings->value("geometry").toByteArray()); | |
ui->splitterHorizontal->restoreState(mySettings->value("splitterStateHorizontal").toByteArray()); | |
ui->splitterVertical->restoreState(mySettings->value("splitterStateVertical").toByteArray()); | |
mySettings->endGroup(); | |
} | |
else //if there are no settings to load, then use default values | |
{ | |
//Default values | |
ui->splitterHorizontal->setStretchFactor(0,3); | |
ui->splitterHorizontal->setStretchFactor(1,6); | |
ui->splitterHorizontal->setStretchFactor(2,1); | |
ui->splitterVertical->setStretchFactor(0, 2); | |
ui->splitterVertical->setStretchFactor(1, 1); | |
} |
As you can see, it's all pretty simple and easy to do. The default values are set using QSplitter::setStretchFactor(int index, int value), which uses the relative weight of each stretch factor to adjust the sizes.
Thanks for stopping by,
Chris Konstad
Stretch factors are very, very important to master when designing flexible UI's. You'll find really quickly that using splitters is really hard if you don't understand them. Also, just a tip, but when saving state of widgets, sometimes a foreach loop that uses all the splitters in the UI is way easier. You can use the object name as the key right from the pointer.
ReplyDeleteI learned that the hard way today while banging my head against my desk when my UI was all messed up. XD Yeah, that foreach loop trick is handy. I probably should have done it that way to make it easy to add more QSplitters later. This is my first attempt to make a really user-adjustable UI.
ReplyDeleteIt's a lot more important when you have, say, 8-10 splitters. It just reduces the code bloat immensely. The disadvantage is that you have less control, mainly in the area around the object naming. If you change the name, the key cannot be manually updated.
Delete