Qt World Summit: Submit your Presentation

String list parsing or serializing

  • Hi, I was wondering if Qt has a convenient way to take an object, like QStringList or list of int vectors and output to a file and read it back in.


    QString qs := "( (1,2), (3,4), (5,6))" <--> QStringList list := { "1,2", "3,4", "5,6" } <--or maybe --> vector< vector<int>> vlist := { {1,2}, {3,4}, {5,6} }.

    Doesn't have to be complicated, but I'm thinking boost::serialization might be close to what I want.

    Thanks, in advance.

    [EDIT: code formatting, Volker]

  • There are a number of options for serializing data to a file. The "proper one" would depend on what you intend to use the file for (i.e., whether it's designed to be easily edited by hand, or human readable, or whether it's for data storage, or saving settings data, or whatever.)

    You could look in to QDataStream or QTextStream along with QFile to store the items. Both of those can serialize the main data types in Qt relatively easily. You could also look at QSettings if the intent is to save some data for later.

    Also there's all sorts of options for reading and writing XML files (which are technically text files), should that appeal to you.

    If you can give some more detail as to what you'd like to do, we can probably guide you in the happiest direction!

  • I'm interested in a simple to use and human legible/editable; I consider xml as not human legible; well, not simple.

    A specific one would be
    vector< vector<int>> list = { {1,2}, {3,4,5} }.

    cout << list; //outputs: ( (1,2), (3,4,5) )

    I may also want to convert vector
    @< vector<QString>>@
    or QStringList.

    Thank you!

    [edit: @ tags added, please add them around your code, Eddy]

  • In that case, Qt does not have anything ready-to use that meets your specifications. It can of course be easily used to create something like this.

  • you can overwrite the streaming operators to support std:ostring for QString andf QStringList.
    Then it should work :-)

Log in to reply