4

I'm having difficulty figuring out how to access a Qt Resource File from a function that is not from Qt.

I've read the problem posted at How to access Qt resource data from non-Qt functions but the answer given just gave a workaround on how to include the file in the same directory as the executable, which is not what I want to do. I want to ensure that the file exists and can't be tampered with when my program runs (which is why I want to use the qrc route).

To give you some background: I'm using this in a program that uses OpenCV's Machine Learning Libraries where I need to load a training file. The loading processing takes one argument, a path to an .xml file.

At the moment the only solution I can come up with is every time I load the file I open the qrc file using QFile, then output this file temporarily in the directory with my executable, then load this file using my OpenCV load function, then delete the temporary file from my directory.

This would be a pretty messing workaround that I'd like to avoid. Any solutions to my problem would be greatly appreciated.

Community
  • 1
  • 1
Stanton
  • 904
  • 10
  • 25
  • And isn't there a function where you can supply a buffer of data? Or even better, data in chunks? – peppe Jul 08 '16 at 19:53
  • 3
    From like 3 seconds of Googling and knowing less than zero about OpenCV, looks like that you may want to create a `FileStorage` in MEMORY mode, populate it and use `CvStatModel::read` instead of `load`? – peppe Jul 08 '16 at 19:54
  • Do not understand. Do you want to use .qrc as an input file format? And not to embed it to your program? Why? – ilotXXI Jul 08 '16 at 20:06
  • Possible duplicate of [Read an image from a qrc using imread() of OpenCV](http://stackoverflow.com/questions/33922284/read-an-image-from-a-qrc-using-imread-of-opencv) – Miki Jul 09 '16 at 01:35
  • @ilotXXI I do want to embed the file into the program. The file extension is an .xml and I would add this to my qt resource file (which is a .qrc). My problem was with how to get my file back from the Qt Resources – Stanton Jul 11 '16 at 13:31

1 Answers1

4

Thanks to @peppe for the tips to use cv::FileStorage and CvStatModel::read

Here is the solution I used:

QFile qFile(":/QtResourceFileName.xml");
if (qFile.open(QFile::ReadOnly))
{
    QTextStream qTextStream(&qFile);
    std::string str = qTextStream.readAll().toStdString();

    cv::FileStorage fileStorage(str, cv::FileStorage::MEMORY);

    cv::Ptr<cv::ml::SVM> svm =
        Algorithm::read<cv::ml::SVM>(fileStorage.getFirstTopLevelNode());
}
Stanton
  • 904
  • 10
  • 25