0

I want to use QSettings to save my window's dimensions so I came up with these two functions to save & load the settings:

void MainWindow::loadSettings()
{
    settings = new QSettings("Nothing","KTerminal");
    int MainWidth = settings->value("MainWidth").toInt();
    int MainHeight = settings->value("MainHeight").toInt();
    std::cout << "loadSettings " << MainWidth << "x" << MainHeight << std::endl;
    std::cout << "file: " << settings->fileName().toLatin1().data() << std::endl;
    if (MainWidth && MainHeight)
      this->resize(MainWidth,MainHeight);
    else
      this->resize(1300, 840);
}

void MainWindow::saveSettings()
{
  int MainHeight = this->size().height();
  int MainWidth = this->size().width();

  std::cout << "file: " << settings->fileName().toLatin1().data() << std::endl;
  std::cout << "saveSettings " << MainWidth << "x" << MainHeight << std::endl;

  settings->setValue("MainHeight",MainHeight);
  settings->setValue("MainWidth",MainWidth);

}

Now, I can see the demensions being extracted in saveSettings as expected but no file gets created and hence loadSettings will always load 0 only. Why is this?

stdcerr
  • 13,725
  • 25
  • 71
  • 128

1 Answers1

4

QSettings isn't normally instantiated on the heap. To achieve the desired effect that you are looking for, follow the Application Example and how it is shown in the QSettings documentation.

void MainWindow::readSettings()
{
    QSettings settings(QCoreApplication::organizationName(), QCoreApplication::applicationName());
    const QByteArray geometry = settings.value("geometry", QByteArray()).toByteArray();
    if (geometry.isEmpty()) {
        const QRect availableGeometry = QApplication::desktop()->availableGeometry(this);
        resize(availableGeometry.width() / 3, availableGeometry.height() / 2);
        move((availableGeometry.width() - width()) / 2,
             (availableGeometry.height() - height()) / 2);
    } else {
        restoreGeometry(geometry);
    }
}

void MainWindow::writeSettings()
{
    QSettings settings(QCoreApplication::organizationName(), QCoreApplication::applicationName());
    settings.setValue("geometry", saveGeometry());
}

Also note the use of saveGeometry() and restoreGeometry(). Other similarly useful functions for QWidget based GUIs are saveState() and restoreState() (not shown in the above example).

I strongly recommend the zero parameter constructor of QSettings, and to setup the defaults in your main.cpp, like so:

QSettings::setDefaultFormat(QSettings::IniFormat); // personal preference
qApp->setOrganizationName("Moose Soft");
qApp->setApplicationName("Facturo-Pro");

Then when you want to use QSettings in any part of your application, you simply do:

QSettings settings;

settings.setValue("Category/name", value);
// or
QString name_str = settings.value("Category/name", default_value).toString();

QSettings in general is highly optimized, and works really well.

Hope that helps.


Some other places where I've talked up usage of QSettings:

Using QSettings in a global static class

https://stackoverflow.com/a/14365937/999943

Community
  • 1
  • 1
phyatt
  • 18,472
  • 5
  • 61
  • 80
  • Perfect & Beautiful! Thanks! – stdcerr Oct 09 '16 at 04:04
  • I think that the reason for `QSettings` not working when allocated on the heap, because it is not destructed (unless `delete` is called). And the destructor of `QSettings` is needed to call its [`sync()`](https://doc.qt.io/qt-5/qsettings.html#sync) function. – Mike Oct 09 '16 at 13:27
  • Excellent point. The sync function can be manually called to get it to work on the heap. – phyatt Oct 09 '16 at 13:29