Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QMetaType ID uniqueness



  • Hi,

    I was working on some stuff and started wandering:

    What if I work with custom types registered in Qt's metatype-system and the number/order of registered types changes?

    Registering a metatype means getting a id, simply incrementing an integer starting at 1024. If I keep everything inside my application, I can use QMetaType's template functions to get the correct id for a type and don't have to worry about it.

    But things will be different if my data leaves my application. An example could be storing a custom type in QSettings. If I make a new version of my app and add some types to the metatype-system, the id stored by QSettings(QVariant) might not be the same as in my recompiled app.

    Off course, with QSettings I explicitly cast to the correct type. But if I wanted to use the id to identify the type in cases where the type is only known through the id that wouldn't work (let's say sharing data using a QVariant through shared memory or networks or with dll's (different compilation) ).

    The options I see right know are trying all types in casts and see if I get the correct one or adding a (string) identifier to explicitly name the type myself.

    But I don't like both versions. The id exists for identification, adding a second identification method should be redundant (I wouldn't have incremented a counter but used an id similar to a hash or checksum which or highly unlikely to be identical fro two classnames)

    I would like to some opinions on that matter. Is there some other approach I'm missing or is it possible to set the id yourself or something?


  • Moderators

    @Larvae said in QMetaType ID uniqueness:

    The id exists for identification

    Yes, but this ID was only designed to help Qt internals use user-defined types within a single application. It was not designed as a persistent or a globally unique ID for data exported between different versions of the same application. It doesn't fit your use case, so don't use it.

    The options I see right know are trying all types in casts and see if I get the correct one or adding a (string) identifier to explicitly name the type myself.

    Out of these two, I feel that adding your own ID is the better option. This way, you can robustly identify a type even between versions (just make sure that your future changes to your types maintain backwards compatibility).


Log in to reply