首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Qt5中对QResource文件进行读写?

如何在Qt5中对QResource文件进行读写?
EN

Stack Overflow用户
提问于 2018-09-03 18:59:02
回答 1查看 568关注 0票数 1

奇怪的是,我通过add Existing Files将所需的文件添加到资源中,文件就在那里。我运行qmake ("Build->Run qmake")使文件可用。第一个问题:我不能从输出终端!向文件中写入任何内容,但是当我手动写入文件时,每次运行输出终端时,输出终端都会显示更改。第二个问题:仍然显示QIODevice::read: device not open!下面是我的代码:

代码语言:javascript
复制
#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();
}

下面是代码的输出终端:

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-09-03 19:06:16

保存在qresource中的文件是只读,因为它们是可执行文件的一部分,所以您不能写入或修改它们。

docs:

目前,Qt总是将数据直接存储在可执行文件中,甚至在Windows、macOS和iOS上也是如此,在这些平台上,操作系统为资源提供了本机支持。..。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52148390

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档