QML QSettings和QML - 无法保存/加载设置

时间:2014-03-26 20:40:50

标签: c++ qt4 qml qt5

我做错了什么?我一直在调试控制台 - "错误!无法加载数据"。任何人都可以指出错误吗?也许我创造了糟糕的设置" ?这是一个许可计划问题吗?

 //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);
}

在QML中我使用这个类:

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"
    }

1 个答案:

答案 0 :(得分:2)

QSettings constuctor需要至少2个字符串organization 名称application 名称

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

在您的情况下,只需在main()

中使用以下代码即可

(1)如果您在应用程序的许多位置使用QSettings,则可能需要使用QCoreApplication::setOrganizationName()QCoreApplication::setApplicationName()指定组织名称和应用程序名称,然后使用默认 QSettings构造函数:

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

(1)参考:QSettings