Search code examples
c++qtqt4qsettings

Store settings of qt application using QSettings


Hello I have created an application using qt and I managed to save some of its settings using QSettings.

void DoneIt::writeSettings()
{
    QSettings settings("mycompany", "RightDoneIt");
    settings.beginGroup("DoneIt");
    settings.setValue("size", size());
    settings.setValue("pos", pos());
    settings.endGroup();
}

void DoneIt::readSettings()
{
    QSettings settings("mycompany", "RightDoneIt");
    settings.beginGroup("DoneIT");
    resize(settings.value("size", QSize(400, 400)).toSize());
    move(settings.value("pos", QPoint(200, 200)).toPoint());
    settings.endGroup();
}

That works fine with the window position and size. I have add some widgets in my application using the designer of qt and I would like to save their state too.

One of my widgets is a radio button and I call it radioButtonbnw

How can I save its state (checked or unchecked) ?

What are the best practises ?


Solution

    1. Put them to QButtonGroup.
    2. Use QButtonGroup::setId to set Id for each radio button in this group.
    3. Save the Id of the checked button get by QButtonGroup::checkedId.
    4. Get the pointer of this button using QButtonGroup::button(id) when restore, and call QAbstractButton::setChecked.

    BTW: if you want to saves the current state of mainwindow's toolbars and dockwidgets, use QMainWindow::saveState.