Store settings of qt application using QSettings
Asked Answered
E

2

6

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 ?

Evin answered 17/11, 2010 at 4:27 Comment(0)
Q
10
  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.

Quinn answered 17/11, 2010 at 4:27 Comment(3)
Please, could you be a little bit more precise? its my first time dealing with QbuttonGroup. Can you provide me with some code ? 1000 thanksEvin
Sorry that I'm busy with my project, and didn't have existing code.Quinn
No problem :) here are ur votes and points :) good luck with your projectEvin
O
1

Here an example widget with three QRadioButton

settingspane.h

#ifndef SETTINGSPANE_H
#define SETTINGSPANE_H

#include <QWidget>
#include <QSettings>

class SettingsPane
      : public QWidget
{
    Q_OBJECT

    QSettings *settings;

public:
    explicit SettingsPane(QWidget *parent = nullptr);

public slots:
    void handle_rb_buttonReleased(int id);
};

#endif // SETTINGSPANE_H

settingspane.cpp

#include "settingspane.h"

#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QRadioButton>
#include <QButtonGroup>

SettingsPane::SettingsPane(QWidget *parent)
   : QWidget(parent)
{
    settings = new QSettings();

    auto mlayout = new QVBoxLayout();
    setLayout(mlayout);

    // create some buttons
    auto rb_frst = new QRadioButton("first");
    auto rb_scnd = new QRadioButton("second");
    auto rb_thrd = new QRadioButton("third");
    // container to organize groups of buttons (no visual)
    auto buttonGroup = new QButtonGroup();
    buttonGroup->addButton(rb_frst,0);
    buttonGroup->addButton(rb_scnd,1);
    buttonGroup->addButton(rb_thrd,2);
    // layout buttons for visual representation
    auto rb_layout = new QHBoxLayout();
    rb_layout->addWidget(rb_frst);
    rb_layout->addWidget(rb_scnd);
    rb_layout->addWidget(rb_thrd);
    mlayout->addLayout(rb_layout);

    // use Functor-Based Connection due to overloaded buttonReleased
    connect( buttonGroup,
             SIGNAL(buttonReleased(int)),
             this,
             SLOT(handle_rb_buttonReleased(int)));

    // restore button from settings
    int id = settings->value("buttonGroup").toInt();
    buttonGroup->button(id)->setChecked(true);
}

void
SettingsPane::handle_rb_buttonReleased(int id)
{
    // save new status to the settings
    settings->setValue("buttonGroup", id);
}

Using this in a MainWindow

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

class MainWindow
      : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = 0);
};

#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include "settingspane.h"

#include <QTabWidget>

MainWindow::MainWindow(QWidget *parent)
   : QMainWindow(parent)
{
    auto cwidget = new QTabWidget();
    setCentralWidget(cwidget);

    auto settingsPane = new SettingsPane();
    cwidget->addTab(settingsPane,"Settings");
}

main.cpp

#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}

results in:
enter image description here

Oscillogram answered 23/3, 2018 at 22:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.