QIODevice::write : 设备未打开

QIODevice::write : device not open

我最近一直在尝试使用 Qt 构建一个纸牌游戏,但是我遇到了一个非常奇怪的错误。

QFile file(":/file/02");
file.open(QIODevice::ReadWrite|QIODevice::Truncate);

第二行的return值为false。因此,当我尝试读取或写入文件时,会抛出此异常。 然而,该文件确实存在,并且项目中的一个qrc文件是这样写的:

<qresource prefix="/file">
<file alias="01">data.json</file>
<file alias="02">deck.json</file>

我在添加这个 qrc 文件后也 运行 qmake,但一切都保持不变。我该如何解决?非常感谢。

根据 docs:

The Qt resource system is a platform-independent mechanism for storing binary files in the application's executable. This is useful if your application always needs a certain set of files (icons, translation files, etc.) and you don't want to run the risk of losing the files.

因为它是资源的一部分,文件是只读的,所以如果你用写权限打开它,它总是return false。

简而言之,.qrc 中存储的文件是静态的。