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.

5
  • And isn't there a function where you can supply a buffer of data? Or even better, data in chunks? Commented Jul 8, 2016 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? Commented Jul 8, 2016 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? Commented Jul 8, 2016 at 20:06
  • Possible duplicate of Read an image from a qrc using imread() of OpenCV Commented Jul 9, 2016 at 1: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 Commented Jul 11, 2016 at 13:31

1 Answer 1

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());
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.