Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved]Writing to file doesn't work.
Forum Updated to NodeBB v4.3 + New Features

[Solved]Writing to file doesn't work.

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 3.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Darkmalex
    wrote on last edited by
    #1

    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_OBJECT

    public:
    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&#40;mSource&#41;;
    QString fileContent;
    if ( file.open(QIODevice::ReadOnly&#41; ) {
        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&#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
    
    
        file.close();
    
        return true;
    

    }@

    Thanks, in advance.

    1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #2

      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 ?

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      1 Reply Last reply
      0
      • D Offline
        D Offline
        Darkmalex
        wrote on last edited by
        #3

        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:

        @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

        1 Reply Last reply
        0
        • D Offline
          D Offline
          Darkmalex
          wrote on last edited by
          #4

          For future reference, I found that qrc files are readonly. So fixed.

          1 Reply Last reply
          0
          • A Offline
            A Offline
            ambershark
            wrote on last edited by
            #5

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

            My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved