Qt World Summit: Submit your Presentation

Problem with QSettings

  • Hi guys!
    Can you explain me why this code doesn't work?
    It looks so easy.

    QSettings settings;
    settings.setValue("key", 123);
    qInfo() << settings.value("key");
    return a.exec();

    Thank you

  • Is that your full code about QSettings?
    Then according to the doc:

    QSettings::QSettings(QObject *parent = nullptr)

    Constructs a QSettings object for accessing settings of the application and organization set previously with a call to QCoreApplication::setOrganizationName(), QCoreApplication::setOrganizationDomain(), and QCoreApplication::setApplicationName().
    The scope is QSettings::UserScope and the format is defaultFormat() (QSettings::NativeFormat by default). Use setDefaultFormat() before calling this constructor to change the default format used by this constructor.
    The code

    QSettings settings("Moose Soft", "Facturo-Pro");

    is equivalent to

    QCoreApplication::setOrganizationName("Moose Soft");
    QSettings settings;

    If QCoreApplication::setOrganizationName() and QCoreApplication::setApplicationName() has not been previously called, the QSettings object will not be able to read or write any settings, and status() will return AccessError.

    You could try using a filename when creating QSettings:
    QSettings settings("settings.ini", QSettings::IniFormat);

  • @Bonnie

    Thank you, the problem is gone :)

  • @amy-cpp said in Problem with QSettings:

    the problem is gone :)

    so please don't forget to mark your post as solved!

Log in to reply