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:
images/1.jpg
images/2.jpg
images/3.jpg
images/4.jpg
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 大力的万宝路.
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.
while (dirIterator.hasNext()) {
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
images/1.jpg
images/2.jpg
images/3.jpg
images/4.jpg
main.cpp
#include
#include
#include
#include
int main()
{
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()) { if (name == "file") standardOutput << "file: :/" << inputStream.readElementText() << "n"; } } return 0; } main.pro TEMPLATE = app TARGET = main QT = core SOURCES += main.cpp Build and Run qmake && make && ./main Output file: :/images/1.jpg file: :/images/2.jpg file: :/images/3.jpg file: :/images/4.jpg