[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:


    @#ifndef FILEIO_H
    #define FILEIO_H

    #include <QObject>

    class FileIO : public QObject

    explicit FileIO(QObject *parent = 0);

    Q_INVOKABLE QString read(const QString& Url);
    Q_INVOKABLE bool write(const QString& Url, QString data);

    public slots:

    void error(const QString& msg);

    QString mSource;

    #endif // FILEIO_H


    @#include "fileio.h"
    #include <QFile>
    #include <QTextStream>

    FileIO::FileIO(QObject *parent) :


    QString FileIO::read(const QString& Url)
    mSource = Url;
    if (mSource.isEmpty()){
    emit error("source is empty");
    return QString();

    QFile file&#40;mSource&#41;;
    QString fileContent;
    if ( file.open(QIODevice::ReadOnly&#41; ) {
        QString line;
        QTextStream t( &file );
        do {
            line = t.readLine();
            fileContent += line;
         } while (!line.isNull());
    } 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&#40;mSource&#41;;
        if (!file.open(QIODevice::WriteOnly | QIODevice::Truncate&#41;){
            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
        return true;


    Thanks, in advance.

  • Qt Champions 2017

    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 qml

    Example would be:

    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.

  • Moderators

    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;


Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.