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. Cannot load Custom type using QSettings

Cannot load Custom type using QSettings

Scheduled Pinned Locked Moved Solved General and Desktop
2 Posts 1 Posters 158 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.
  • S Offline
    S Offline
    ScottSimmo
    wrote on last edited by ScottSimmo
    #1

    I have a custom type which has been setup correctly, I've registered the type and stream operators prior to using QVariant and used Q_DECLARE_METATYPE(CustomClass) in the class' header file. However, when I use

    QVariant value = settings->value("custom_type")

    It looks as though value has correctly deserialised into the type, but no data was loaded as everything is set to 0 despite the fact another part of the program can load the exact same data fine (It's a large project and I can't find any differences). When I try and set the property, it returns false though and can convert also returns false.

    setProperty(prop, value) // Where prop is of type custom_type

    Are there any known issues that could cause this or have I missed a step before trying to load the custom type?

    1 Reply Last reply
    0
    • S Offline
      S Offline
      ScottSimmo
      wrote on last edited by ScottSimmo
      #2

      The issue we were having was qRegisterMetaType was being called too far in for QSettings to use it. In hindsight, the documentation does mention to include it in the main/constructor but we were abit mislead by the working part of the program. I hope this helps anybody reading this, below is the best way we found to ensure the metatypes are always registered:

      Header File

      static const bool _registered;
      
      static bool register_types()
      {
        bool result;
        result = qRegisterMetaType<CustomType>("CustomType");
        qRegisterMetaTypeStreamOperators<CustomType>("CustomType");
        return result;
      }
      

      Source File

      const bool CustomType::_registered = CustomType::register_types();
      
      1 Reply Last reply
      1

      • Login

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