我正在使用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
的main.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();
}
答案 0 :(得分:2)
很可能是因为你在函数中泄漏了QSettings
'内存和资源,所以第一次调用会使后来的失败,因为占用了文件句柄,或者是shomething。 / p>