2014-03-26 3 views
1

내가 뭘 잘못 했습니까? 나는 항상 디버그 콘솔에있다. "오류! 데이터를로드 할 수 없다". 누구든지 오류를 지적 할 수 있습니까? 나쁜 "설정"을 만들 수 있습니까? 허가권 문제입니까?QML QSettings 및 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" 
    } 

답변

2

QSettings constructor에 : //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에서 내가 그런이 클래스를 사용

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

최소 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