[Solved]Writing to file doesn't work.
-
Hi, for my QML project I need a simple IODevice to work with files so I took "this one from Nokia Dev":http://developer.nokia.com/community/wiki/Reading_and_writing_files_in_QML
But I tweaked it a little, for purposes of my work.
Reading from file works like a charm(this proves, that there is no "wrong path to file" problem"), but writing to file is broken, and I can't find a reason why.Here is the code:
fileio.h
@#ifndef FILEIO_H
#define FILEIO_H#include <QObject>
class FileIO : public QObject
{
Q_OBJECTpublic:
explicit FileIO(QObject *parent = 0);Q_INVOKABLE QString read(const QString& Url); Q_INVOKABLE bool write(const QString& Url, QString data);
public slots:
signals:
void error(const QString& msg);private:
QString mSource;
};#endif // FILEIO_H
@fileio.cpp
@#include "fileio.h"
#include <QFile>
#include <QTextStream>FileIO::FileIO(QObject *parent) :
QObject(parent)
{}
QString FileIO::read(const QString& Url)
{
mSource = Url;
if (mSource.isEmpty()){
emit error("source is empty");
return QString();
}QFile file(mSource); QString fileContent; if ( file.open(QIODevice::ReadOnly) ) { QString line; QTextStream t( &file ); t.setCodec("UTF-8"); do { line = t.readLine(); fileContent += line; } while (!line.isNull()); file.close(); } else { emit error("Unable to open the file"); return QString(); } return fileContent;
}
bool FileIO::write(const QString& Url, QString data)
{
mSource = Url;
if (mSource.isEmpty()){
emit error("source is empty");
return false;}QFile file(mSource); if (!file.open(QIODevice::WriteOnly | QIODevice::Truncate)){ emit error("Error"); return false;} QTextStream out(&file); out << data; //emit error("data:" + data); //This one was used to debug, Yet no errors were emmited when Write is called file.close(); return true;
}@
Thanks, in advance.
-
I dont' see issue with code. What is the data written ? what is broken here ? What is the data you are dumping from QML ? Also can you give sample of how you are calling read and write from QML ?
-
I needed my app to load settings from external file (I cant use QSettings, as I wish to give user access these settings by external script or through text editor when app is not launched). So for every setting I have a file with a single Utf-8 string, which is loaded into qml (smth like @property string interfacecolor1 : myFile.read("://settings/color1");@), and it works.
But I also want to change settings in qmlExample would be:
@TextField{
id:fieldsurname
Layout.fillWidth: true
text: myFile.read("://settings/surname"); //Shows user current setting, works perfectly
onTextChanged: {console.log(fieldsurname.text); //I just check if textfield behaves as supposed, returns what I expect from it.
myFile.write("://settings/surname", fieldsurname.text); //Write text from textfield (it will be for an embeded platform, so I probably need to change setting every new char)
surname = myFile.read("://settings/surname"); //I write new setting to property from new text from file
console.log(myFile.read("://settings/surname")) //Returns an unchanged string
}
}@Also forgot to mention, that manualy editing files also works, settings are changed accordingly, app behaves as it should
-
For future reference, I found that qrc files are readonly. So fixed.
-
Well, first off you can lose the QIODevice::Truncate from your open in write since it is implied by QIODevice::WriteOnly.
Secondly, you may be having a trouble because you are using a binary file with a text stream.
Try doing this:
@
QFile file(mSource);
if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
return false;QTextStream out(&file);
out << data << endl;file.close();
@