[SOLVED] QSettings with QList of a custom class



  • Hello!

    I'm relatively new to Qt (less than a month) and to this forums. I apologize in advance for any mistake.

    I'm working on a project for my degree and I'm having a problem.
    I have a QList of a custom class objects: (QList<Instructions> instructions)
    All my project goes around this QList and so far it is working great.
    My only problem is saving this QList so I can load it when the application launches.
    I've read and seen tutorials about QSettings and I've managed to save/load a type int variable but I'm having problem with this QList variable.

    I've seen this tutorial for a QList<QTime> but I can't figure out how to make this work with my Instruction class objects:
    QVariantList timeList;
    foreach(QTime t,times)
    timeList<<t;
    settings.setValue("timeList", timeList);

    QVariantList reading = settings.value("timeList").toList();
    QList<QTime> times;
    foreach(QVariant v, reading)
    times << v.toTime();

    I understand the concept behind QSettings and the previous tutorial but I really can't adapt it to my problem.
    I would appreciate if someone could help me.

    Thank you!



  • hey Welcome to Qt!

    For saving complex type that are not supported directly with QSettings, you could look at saving your custom object to a xml file.
    Have a look at this if this is an option for you.

    You can always use QSettings but I found that for me, it was better to learn how to write/load an xml file if you plan to use this approach on the long term.

    Good luck!


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    You need to make your custom type known to QVariant using Q_DECLARE_METATYPE before you can using with e.g. QSettings.



  • Hi,
    Thank you both for your suggestions!

    As this project is intended for industrial purposes I think XML is a great solution.
    I followed some tutorials on how to use XML on Qt and managed to make it work (as well as understand) pretty well for what I intended to do.

    I'm leaving here the tutorials I followed in case someone stumbles upon this thread with the same problem: (I apologize if this is not allowed)
    https://www.youtube.com/watch?v=NXGE5XUrRSI
    https://www.youtube.com/watch?v=NzQwJdcdRKE
    I had to make some changes to my project but it's easy to understand.

    Thank you, once again!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.