短而烦人的问题;我无法访问qt资源文件(又名.qrc)中定义的任何内容。我按照qt语言创建了一个名为TextFinder.According的小部件应用程序,我创建了所有必要的文件,并完成了所有的指令,但我无法访问qrc内容。在项目文件夹中,我有以下文件:
TextFinder
resources
input.txt
main.cpp
textfinder.cpp
textfinder.h
TextFinder.pro
TextFinder.pro.user
TextFinder.qrc
textfinder.uiqrc文件的内容如下:
<RCC>
<qresource prefix="/res">
<file>resources/input.txt</file>
</qresource>
</RCC>要访问该文件,我在编辑器中打开qrc,右键单击该文件,并选择复制资源路径到剪贴板选项。这就产生了":/res/resources/input.txt“。所以我把这个输入到我的函数中来打开文件。此函数如下所示:
void TextFinder::loadTextFile()
{
QFile inputFile(":/res/resources/input.txt");
inputFile.open(QIODevice::ReadOnly);
if (inputFile.isOpen())
{
QTextStream txtStream(&inputFile);
QString contents = txtStream.readAll();
inputFile.close();
ui->textEdit->setPlainText(contents);
QTextCursor cursor = ui->textEdit->textCursor();
cursor.movePosition(QTextCursor::Start, QTextCursor::MoveAnchor, 1);
}
else
{
throw std::runtime_error("Resource file may be wrong?");
}
}当我运行应用程序时,会抛出runtime_error,它告诉我它无法打开文件。在项目文件中,qrc文件的定义如下:
RESOURCES += \
TextFinder.qrc这里面出什么问题了?有人能指出我做错了什么吗?
你好,乔
发布于 2014-02-03 12:59:14
根据Qt资源系统文档:
还可以使用qresource标记的前缀属性为.qrc文件中的所有文件指定路径前缀: 图像/cut.png 在这种情况下,文件可以访问为:/myresources/cut-img.png。
因此,当前缀存在时,子路径被切断。
https://stackoverflow.com/questions/21502920
复制相似问题