Important: Please read the Qt Code of Conduct -

Best way to save program data

  • Hi all,

    I will shortly explain my situation.
    I have written a qt application to read data from a can bus. When specific data arrives at the embedded system, I would like to run certain piece of code. The user of the system should be able to program this code graphically.

    The problem I'm facing now:
    What is the best way to save those settings (graphical code)?

    • Should I use A small database
    • Can I put it into a .ini file
    • Can I put it into a text file
    • Other possibilities?

    Thanks in advance,
    Kind regards

  • @TMJJ001 If you have some "program code" then it can be represented as "tree". Qt has very powerful XML support. Learn it and save your data in XML file as tree. It is fast enough even for large trees. I used it in my other project to save user created complex schemes.

    But this does not relate to Mobile and Embedded...

  • Lifetime Qt Champion


    • It can be stored in a database, but I would not do it this way
    • No! Ini files are configuration files. What you describe sounds like application data, not configuration
    • Sure you can if you define a format
    • XML as suggested by @Gourmet, your own text/binary format, any other format already available for "graphical code"

  • @Gourmet

    Thanks for the reply, are you able to show me the direction to go?
    Something like a tutorial or something?
    I was looking on the internet but I'm unable to get started.

    Kind regards

  • Qt Champions 2017

    I, personally, would go with a small SQLite database. Even large trees can efficiently be represented in linear sets (as an SQL database) by keeping the left and right depth search index as metadata. Not to mention this way you don't need to define the XML structure and write the parsing. As for graphs, that's entirely different kettle of fish, so perhaps you should elaborate on what this graphical code would need to actually store.

  • @Gourmet

    Hi, I try to go with xml.

    I was looking for some good tutorials on how to learn it. I found the following:

    Now when I try to load, I can't. I can't find my mistake.
    Anybody an idea?

        // DOM reading xml file
        QDomDocument document("MachineSetting");
        //load xml file
        QFile file("MachineSetting.xml");
        if(! | QIODevice::Text))
            qDebug() <<"Failed to open file";
                    qDebug()<<"Failed to load document";

    After running the function it says:
    Failed to load document.

    Any idea?

    I a

  • Lifetime Qt Champion


    setContent as several more parameters that allow you to get information about what is going wrong. You should use them to check what is going on.

  • Lifetime Qt Champion

    @TMJJ001 Can you show the content of MachineSetting.xml ?

  • Moderators

    @TMJJ001 said in Best way to save program data:

    What is the best way to save those settings (graphical code)?

    to make the suggestions complete i would say to use binary format :D
    You can use QDataStream class for that.
    Save your data variables, lists, etc. into a file with QDataStream. Then read them back in in the same order you've written them.

    For custom types you need to create the corresponding stream operators (see the docs).

  • @jsulm

    Sorry for the late reply.
    There was an error in the .xml file and not in my code!
    So I managed to solve the error.

    Code works like a charm!

Log in to reply