gpt4 book ai didi

c++ - Qsettings 只能存储一次值吗?

转载 作者:行者123 更新时间:2023-11-28 02:01:51 32 4
gpt4 key购买 nike

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

文件io.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();
}

文件io.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();
}

最佳答案

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

关于c++ - Qsettings 只能存储一次值吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39126360/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com