Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] QSettings with QList of a custom class
QtWS25 Last Chance

[SOLVED] QSettings with QList of a custom class

Scheduled Pinned Locked Moved General and Desktop
qsettingsqlistclass
4 Posts 3 Posters 2.8k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • X Offline
    X Offline
    XADOOO
    wrote on last edited by XADOOO
    #1

    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!

    1 Reply Last reply
    0
    • M Offline
      M Offline
      maximus
      wrote on last edited by
      #2

      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!


      Free Indoor Cycling Software - https://maximumtrainer.com

      1 Reply Last reply
      1
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        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.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        1
        • X Offline
          X Offline
          XADOOO
          wrote on last edited by
          #4

          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!

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved