本文介绍了在Qt中获取qrc文件的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我想知道如何访问qrc文件中的文件的路径,以便将它们用作数组中的字符串。 qrc文件的示例是: <!DOCTYPE RCC>< RCC version =1.0> ; < qresource prefix => < file> images / 1.jpg< / file> < file> images / 2.jpg< / file> < file> images / 3.jpg< / file> < file> images / 4.jpg< / file> < / qresource> < / RCC> 我想以下列方式使用它: for(int i = 0; i { path = image_path [i] } 其中path是稍后可用于访问各个图像的qlist。 解决方案似乎有一个简单的方法使用 QDirIterator 。 如果您有一个名为:的目录,在当前工作目录中,并且期望将来进行解析。无论如何,现在不应该是一个问题。 QStringList imageFileList; QDirIterator dirIterator(:,QDirIterator :: Subdirectories); while(dirIterator.hasNext()){ QFileInfo fileInfo = it.fileInfo(); if(fileInfo.isFile())//不要向列表中添加目录 imageFileList.append(it.next()); } 或者,这需要相当多的参与,但我认为它工作。恐怕没有更方便的方式写这篇文章。 main.qrc <!DOCTYPE RCC>< RCC version =1.0> < qresource prefix => < file> images / 1.jpg< / file> < file> images / 2.jpg< / file> < file> images / 3.jpg< / file> < file> images / 4.jpg< / file> < / qresource> < / RCC> main.cpp #include< QXmlStreamReader> #include< QString> #include< QFile> #include int main() { QTextStream standardOutput(stdout); QFile文件(main.qrc); if(!file.open(QIODevice :: ReadOnly | QIODevice :: Text)){ standardOutput< 文件打开错误:< file.errorString()<< \\\; return 1; } QXmlStreamReader inputStream(& file); while(!inputStream.atEnd()&&!inputStream.hasError()){ inputStream.readNext(); if(inputStream.isStartElement()){ QString name = inputStream.name()。toString(); if(name ==file) standardOutput<< file::/< inputStream.readElementText()<< \\\; } } return 0; } main.pro TEMPLATE = app TARGET = main QT = core SOURCES + = main.cpp 构建并运行 qmake&制作&& ./main 输出 file::/images/1.jpg file::/images/2.jpg file::/images/3.jpg file::/ images / 4.jpg I want to know how to access the paths of files in the qrc file in order to use them as strings in an array.An example of qrc file is: <!DOCTYPE RCC><RCC version="1.0"> <qresource prefix=""> <file>images/1.jpg</file> <file>images/2.jpg</file> <file>images/3.jpg</file> <file>images/4.jpg</file> </qresource> </RCC>I want to use it in the following manner: for(int i=0;i<4;i++) { path=image_path[i]; }where path is an qlist that can be later used for accessing the respective images. 解决方案 There seems to be a simple way of doing it using QDirIterator.It might break if you have a directory called ":" in the current working directory and you expect that to be parsed instead in the future. Anyhow, that should not be a concern for now.QStringList imageFileList;QDirIterator dirIterator(":", QDirIterator::Subdirectories);while (dirIterator.hasNext()) { QFileInfo fileInfo = it.fileInfo(); if (fileInfo.isFile()) // Do not add directories to the list imageFileList.append(it.next());}Alternatively, this requires quite a bit of involvement, but I think it works. I am afraid there is no more convenient way as of writing this.main.qrc<!DOCTYPE RCC><RCC version="1.0"> <qresource prefix=""> <file>images/1.jpg</file> <file>images/2.jpg</file> <file>images/3.jpg</file> <file>images/4.jpg</file> </qresource></RCC>main.cpp#include <QXmlStreamReader>#include <QString>#include <QFile>#include <QTextStream>int main(){ QTextStream standardOutput(stdout); QFile file("main.qrc"); if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { standardOutput << "File open error:" << file.errorString() << "\n"; return 1; } QXmlStreamReader inputStream(&file); while (!inputStream.atEnd() && !inputStream.hasError()) { inputStream.readNext(); if (inputStream.isStartElement()) { QString name = inputStream.name().toString(); if (name == "file") standardOutput << "file: :/" << inputStream.readElementText() << "\n"; } } return 0;}main.proTEMPLATE = appTARGET = mainQT = coreSOURCES += main.cppBuild and Runqmake && make && ./mainOutputfile: :/images/1.jpgfile: :/images/2.jpgfile: :/images/3.jpgfile: :/images/4.jpg 这篇关于在Qt中获取qrc文件的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 07-29 23:46