Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I include a text file to a qt application?

Tags:

file

xml

qt

I have text file from which I need to get data by line by line. So if my application is launched, it can read from the text file to show the information. But I don't want to supply my text file separately along with my application. How to do this? And well I have to do this using Qt!

I heard like using xml will be a better and easy way to accomplish this.

like image 785
defiant Avatar asked Feb 16 '11 13:02

defiant


2 Answers

You have to add a qt resource file (.qrc) to your project

It might look like this:

<RCC>
    <qresource prefix="/">
        <file>file.xml</file>
        <file>files/file2.xml</file>
    </qresource>
</RCC>

After that you have to add that resource file to your project file (.pro)

Like this for example:

RESOURCES += myqrcfile.qrc

After that you can use that file in your code by using the character ':' to refer to the file

Maybe like this:

QFile data(":/file.xml");
//or
QFile data(":/files/file2.xml");
//etc...

Remember that the path that you define for the file(in the qrc) must correspond to the file's location in the filesystem as well.

Hope this helps, for more information I suggest you read the link to the documentation that Gorkem Ercan posted.

like image 71
Gerstmann Avatar answered Oct 04 '22 03:10

Gerstmann


Qt Resource System is what you are looking for.

like image 23
Gorkem Ercan Avatar answered Oct 04 '22 04:10

Gorkem Ercan