Shravan Singh Shravan Singh - 3 months ago 21
C++ Question

Qsettings able to store value only once?

I am using Qsetting to store the last used values. My code works for only one field. That means it only works once when I am applying the same logic for the second go round it doesnt do that.

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

Answer

Most probably it's because you're leaking QSettings' memory and resources in your functions, so the first call makes later ones fail, because of occupied file handle, or shomething.