Question

What I do wrong ? I always have in debug console-"error! cannot load data". Can anyone point out an error? Maybe I create bad "settings" ? It's a permission program problem?

 //main.cpp
    #include <QtGui/QGuiApplication>
    #include "qtquick2applicationviewer.h"
    #include <QQmlContext>
    #include <QSettings>
    #include "settings.h"

int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);

    QtQuick2ApplicationViewer viewer;

    viewer.setMainQmlFile(QStringLiteral("qml/kon/main.qml"));

    Settings* settings = new Settings();

    viewer.rootContext()->setContextProperty("settings", settings);

    viewer.showExpanded();

    return app.exec();
}

//settings.h

#ifndef SETTINGS_H
#define SETTINGS_H

#include <QObject>
#include <QSettings>

class Settings : public QSettings {
  Q_OBJECT
public:
  Settings(QObject *parent = 0);
  virtual ~Settings();

  Q_INVOKABLE
  void setValue(const QString &key, const QVariant &value);

  Q_INVOKABLE
  void setValueIfNotSet(const QString &key, const QVariant &value);

  Q_INVOKABLE
  QVariant value(const QString &key, const QVariant &defaultValue);

  Q_INVOKABLE
  bool boolValue(const QString &key, const bool defaultValue);

  Q_INVOKABLE
  void initToDefaults();

signals:
  void settingChanged(const QString& key);
};

#endif // SETTINGS_H

//settings.cpp

#include "settings.h"

Settings::Settings(QObject* parent) :
  QSettings(parent) {

}

Settings::~Settings() {
}

QVariant Settings::value(const QString &key, const QVariant &defaultValue = QVariant()) {
  return QSettings::value(key, defaultValue);
}

bool Settings::boolValue(const QString &key, bool defaultValue) {
  return QSettings::value(key, defaultValue).toBool();
}

void Settings::setValue(const QString &key, const QVariant &value) {

  // change the setting and emit a changed signal
  // (we are not checking if the value really changed before emitting for simplicity)
  QSettings::setValue(key, value);
  emit settingChanged(key);
}

void Settings::setValueIfNotSet(const QString &key, const QVariant &value) {

  // change the setting and emit a changed signal
  if( !QSettings::contains(key) ) {
    QSettings::setValue(key, value);
    // (we are not checking if the value really changed before emitting for simplicity)
    emit settingChanged(key);
  }
}

void Settings::initToDefaults() {
  setValueIfNotSet("test", true);
}

And in QML i use this class like that:

Button
        {
            id:button1
        nazwa: "Set value"
        onClicked: settings.setValue("1","adskmmads")
    }

    Button
    {
        id:button2
        onClicked: console.log(settings.value("1","error! cannot load data"))
        nazwa: "Load value and show"
    }
Was it helpful?

Solution

QSettings constuctor needs at least 2 strings organization name and application name

QSettings::QSettings(const QString & organization, const QString & application = QString(), QObject * parent = 0);

In your case just use this code below in your main()

(1)If you use QSettings from many places in your application, you might want to specify the organization name and the application name using QCoreApplication::setOrganizationName() and QCoreApplication::setApplicationName(), and then use the default QSettings constructor:

QCoreApplication::setOrganizationName("MySoft");
QCoreApplication::setOrganizationDomain("mysoft.com");
QCoreApplication::setApplicationName("Star Runner");
...
QSettings settings; //default constructor 

(1)reference: QSettings

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top