很奇怪,我通过以下方式将所需的文件添加到资源中添加现有文件...,文件就在那里。我运行 qmake (“构建->运行 qmake”)以使文件可用。
第一期:我无法从输出终端向文件写入任何内容!但是当我手动写入文件时,每次运行它时输出终端都会显示更改。第二个问题:它仍然说QIODevice::read:设备未打开!
这是我的代码:
#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:
文件保存在资源 http://doc.qt.io/qt-5/resources.html are 只读因为它们是可执行文件的一部分,所以您无法编写或修改它们。
docs: http://doc.qt.io/qt-5/resources.html#compiled-in-resources
目前,Qt 始终将数据直接存储在可执行文件中,即使在操作系统提供资源本机支持的 Windows、macOS 和 iOS 上也是如此。 ...
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)