如何在Qt 5中读写QResource文件? [英] How to write and read to/from a QResource file in Qt 5?
问题描述
奇怪的是,我通过 Add Existing Files ... 将所需的文件添加到资源中,文件在那里.我运行qmake(构建->运行qmake")以使文件可用. 第一个问题: 我无法从输出终端将任何内容写入文件! 但是,当我手动写入文件时,输出终端会在每次运行时显示更改它.第二个问题:它仍然显示 QIODevice :: read:设备未打开 ! 这是我的代码:
It's strange, I add desired file into the resources via Add Existing Files..., the file is there. I run qmake ("Build->Run qmake") to make the file available. The first issue: I can't write anything into the file from output terminal! But when I manually write into the file, the output terminal shows the change every time I run it. Second issue: it still says QIODevice::read: device not open ! Here's my code:
#include <QCoreApplication>
#include <QDebug>
#include <QFile>
#include <QString>
#include <QTextStream>
#include <iostream>
void wFile(QString Filename)
{
QFile nFile(Filename);
QTextStream str(&nFile);
qDebug() << "what do you want to write in the desired file: ";
str.readLine();
if (!nFile.open(QFile::WriteOnly | QFile::Text))
{
qDebug() << "could not open the file";
return;
}
nFile.flush();
nFile.close();
}
void read (QString Filename){
QFile nFile(Filename);
if(!nFile.open(QFile::ReadOnly | QFile::Text))
{
qDebug() << "could not open file for reading";
return;
}
QTextStream in(&nFile);
QString nText = in.readAll();
qDebug() << nText;
nFile.close();
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QString nFilename =":/MyFiles/DocumentArminV.txt";
wFile(nFilename);
read(nFilename);
return a.exec();
}
这是代码的输出端子:
And here's output terminal of the code:
推荐答案
The files saved in a qresource are read-only since they are part of the executable so you can not write or modify them.
当前,Qt始终将数据直接存储在可执行文件中,即使在Windows,macOS和iOS上,操作系统也提供对资源的本机支持. ...
这篇关于如何在Qt 5中读写QResource文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!