Important: Please read the Qt Code of Conduct -

Cannot load Custom type using QSettings

  • 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?

  • 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");
      return result;

    Source File

    const bool CustomType::_registered = CustomType::register_types();

Log in to reply