Qsettings 只能存储一次价值?

Posted

技术标签:

【中文标题】Qsettings 只能存储一次价值?【英文标题】:Qsettings able to store value only once? 【发布时间】:2016-08-24 14:39:11 【问题描述】:

我正在使用 Qsetting 来存储上次使用的值。我的代码只适用于一个领域。这意味着当我在第二轮应用相同的逻辑时它只工作一次,它不会这样做。

fileio.cpp

#include "fileio.h"
#include <QSettings>
#include <QStandardPaths>
#include <Qtandroid>
#include <QtAndroidExtras/QAndroidJniObject>
#include <QAndroidJniEnvironment>
#include <QtGui>
#include <QDebug>

FileIo::FileIo(QObject *parent)
    : QObject(parent)

    connect(this, SIGNAL(maxrpmChanged()), this, SLOT(writeSettings()));
    connect(this, SIGNAL(rampupChanged()), this, SLOT(writeSettings()));

//for Maxrpm
void FileIo::setMaxrpm(const QString &a) 
    if (a != m_maxrpm) 
        m_maxrpm = a;
        emit maxrpmChanged();
    

QString FileIo::getMaxrpm() const return m_maxrpm;

//for Filename
void FileIo::setFilename(const QString &a) 

    if (a != m_filename) 
        m_filename = a;
        emit filenameChanged();
    

QString FileIo::getFilename() const return m_filename;

//for Rampup
void FileIo::setRampup(const QString &a) 
    if (a != m_rampup) 
        m_rampup = a;
        emit rampupChanged();
    

QString FileIo::getRampup() const return m_rampup;



void FileIo::readSettings(const QString &temp)

    QString m_path ;
    QString m_filename;
    QSettings * p_settings;
    QSettings settings("BlueSparq","motorapp");
    m_path = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) ;
    m_filename = temp ;
    p_settings = new QSettings(m_path + "/"+ m_filename,QSettings::IniFormat) ;
    setMaxrpm(p_settings->value("target_RPM",QString("900")).toString());
    qDebug() <<m_maxrpm;
    //for Rampup
    setRampup(p_settings->value("rampup","250").toString());
    qDebug() <<m_rampup;


void FileIo::writeSettings()

    QString m_path ;
    QSettings * p_settings;
    QSettings settings("BlueSparq","motorapp");
    m_path = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) ;
    p_settings = new QSettings(m_path + "/"+ m_filename,QSettings::IniFormat) ;
    p_settings->setValue("target_RPM",m_maxrpm);
    qDebug() << m_rampup;
    p_settings->setValue("rampup",m_rampup);
    qDebug() << m_rampup;
    p_settings->sync();

fileio.h

#ifndef FILEIO_H
#define FILEIO_H
#include <QObject>

class FileIo : public QObject

    Q_OBJECT
    Q_PROPERTY(QString maxrpm READ getMaxrpm WRITE setMaxrpm NOTIFY maxrpmChanged)
    Q_PROPERTY(QString filename READ getFilename WRITE setFilename NOTIFY filenameChanged)
    Q_PROPERTY(QString rampup READ getRampup WRITE setRampup NOTIFY rampupChanged)
public:
    explicit FileIo(QObject *parent = 0);

    //For MaxRPM
    void setMaxrpm(const QString &a);
    QString getMaxrpm() const;

    //For Filename
    void setFilename(const QString &a);
    QString getFilename() const;

    //For Ramup
    void setRampup(const QString &a);
    QString getRampup() const;

    Q_INVOKABLE void writeSettings();
    Q_INVOKABLE void readSettings(const QString &temp);

signals:
    void maxrpmChanged();
    void filenameChanged();
    void rampupChanged();



public slots:


private slots:


private:
    QString m_maxrpm;
    QString m_filename;
    QString m_rampup;

;


#endif // FILEIO_H

ma​​in.cpp

#include <QtGui>
#include <QtQuick>
#include "sertransclient.h"
#include "fileio.h"
QObject *object;

int main(int argc, char *argv[])

    QGuiApplication app(argc, argv);
    //added now
    QQuickView view;

    QObject::connect((QObject*)view.engine(), SIGNAL(quit()), &app, SLOT(quit()));

    qRegisterMetaType<SerTransClient::BaudRate>("SerTransClient::BaudRate");
    qRegisterMetaType<SerTransClient::DataBits>("SerTransClient::DataBits");
    qRegisterMetaType<SerTransClient::Parity>("SerTransClient::Parity");
    qRegisterMetaType<SerTransClient::StopBits>("SerTransClient::StopBits");
    qRegisterMetaType<SerTransClient::FlowControl>("SerTransClient::FlowControl");
    qmlRegisterType<SerTransClient>("sertransclient", 1, 0, "SerTransClient");
    qmlRegisterType<FileIo>("fileIo", 1, 0, "FileIo");
    view.setResizeMode(QQuickView::SizeRootObjectToView);
    view.setSource(QUrl(QStringLiteral("qrc:/main.qml")));
    view.show();
    object = view.rootObject();
    //



    return app.exec();

【问题讨论】:

【参考方案1】:

很可能是因为您在函数中泄漏了QSettings 的内存和资源,所以第一次调用会导致后面的调用失败,因为文件句柄被占用或其他原因。

【讨论】:

以上是关于Qsettings 只能存储一次价值?的主要内容,如果未能解决你的问题,请参考以下文章

从 QSettings 派生并存储 QIcon

如何使用 QSettings 存储 Python 字典

QSettings 类

不同类型的 QSettings

QSettings使用方法

在跨平台环境中是不是有必要的安全方式在 QSettings 中存储数据?