写入文件在Qt5/Qml中不起作用

Writing to file doesn’t work in Qt5/Qml

本文关键字:Qml 不起作用 Qt5 文件      更新时间:2023-10-16

对于我的QML项目,我需要一个简单的IODevice来处理文件,所以我从Nokia Dev拿走了这个但为了我的工作,我稍微调整了一下。从文件中读取就像一个魅力(这证明,没有"错误的路径到文件"问题),但写入文件是坏的,我找不到一个原因。

代码如下:

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

我需要我的应用程序从外部文件加载设置(我不能使用QSettings,因为我希望让用户通过外部脚本或通过文本编辑器访问这些设置,当应用程序未启动时)。因此,对于每个设置,我都有一个带有单个Utf-8字符串的文件,它被加载到qml(例如

)中。
property string interfacecolor1 : myFile.read("://settings/color1");

),它可以工作。但是我还想更改qml中的设置例如:

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
}
            }

还忘了说,手动编辑文件也可以,设置也会相应地改变,应用程序的行为也会像它应该的那样。

那么问题是:出了什么问题?

Ps:这是这个问题在qt项目上的重复,但是这个问题很匆忙,我需要尽快得到答案。

您可以使用QSettings来保存应用程序的设置:

QSettings settings("organizationName","applicationName");
settings.setValue("settings/surname",fieldsurname.text);

或者阅读它们:

surname = settings.value("settings/surname","").toString();

但是如果有必要使用文件(您可能希望使用文件从其他设备导入设置),您应该注意Qt资源中的文件是只读的。因此,如果你想更改它,你应该首先将文件复制到某个位置:

QFile dfile("://settings/surname");
if (dfile.exists())
{
     dfile.copy("./surname");
     QFile::setPermissions("./surname",QFile::WriteOwner | QFile::ReadOwner);
}

Qrc文件是只读的。你不能给他们写信。您必须将文件放入实际的文件系统中,并从/到那里读写它。