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. QMetaType ID uniqueness
Forum Updated to NodeBB v4.3 + New Features

QMetaType ID uniqueness

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 467 Views 1 Watching
  • 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.
  • L Offline
    L Offline
    Larvae
    wrote on last edited by
    #1

    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?

    JKSHJ 1 Reply Last reply
    0
    • L Larvae

      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?

      JKSHJ Offline
      JKSHJ Offline
      JKSH
      Moderators
      wrote on last edited by JKSH
      #2

      @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).

      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

      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