gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 04:03:10 26 4
gpt4 key购买 nike

对于我的 QML 项目,我需要一个简单的 IODevice 来处理文件,所以我从 Nokia Dev 中获取了这个。但为了我的工作,我稍微调整了一下。从文件中读取非常有效(这证明,不存在“文件路径错误”问题),但是写入文件时出现问题,我找不到原因。

代码如下:

文件io.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

文件io.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
}
}

还忘了提一下,手动编辑文件也可以,设置会相应更改,应用会正常运行。

所以问题是:哪里出了问题?

注意:这是this question on qt-project的拷贝但问题被埋没了,我需要尽快得到答案。提前致谢。

最佳答案

您可以使用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);
}

关于c++ - 写入文件在 Qt5/Qml 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24659736/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com