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 alias in Qt6

QMetaType alias in Qt6

Scheduled Pinned Locked Moved Unsolved General and Desktop
metatypeqt5qt6
8 Posts 3 Posters 2.0k Views 2 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.
  • crueeggC Offline
    crueeggC Offline
    crueegg
    wrote on last edited by
    #1

    I'm migrating a Qt5.15 client/server to Qt6.5. QVariant::fromValue() returns a different type/name for aliased types. The appended sample code returns no longer the typedef name MyClassList in Qt6 with

    using MyClassList = QList<MyClass>;
    Q_DECLARE_METATYPE(MyClassList)
    

    Output:

    Qt 5.15.2
    QVariant::fromValue(MyClassList()).typeName=MyClassList
    QMetaType(qMetaTypeId<MyClassList>()).name=MyClassList
    
    Qt 6.5.0
    QVariant::fromValue(MyClassList()).typeName=QList<MyClass>
    QMetaType(qMetaTypeId<MyClassList>()).name=QList<MyClass>
    

    Code:

    #include <QCoreApplication>
    #include <QList>
    #include <QMetaProperty>
    #include <QVariant>
    
    class MyClass {};
    Q_DECLARE_METATYPE(MyClass);
    
    using MyClassList = QList<MyClass>;
    Q_DECLARE_METATYPE(MyClassList);
    
    int main(int argc, char *argv[])
    {
      QCoreApplication a(argc, argv);
      qInfo("Qt %s", QT_VERSION_STR);
    
      QVariant v = QVariant::fromValue(MyClassList());
      qInfo("QVariant::fromValue(MyClassList()).typeName=%s", v.typeName());
    
      QMetaType mtMcl = QMetaType(qMetaTypeId<MyClassList>());
      qInfo("QMetaType(qMetaTypeId<MyClassList>()).name=%s", qPrintable(mtMcl.name()));
    
      return 0;
    }
    
    • Is there a way QVariant uses the aliased name MyClassList?

    • How do I get the Aliased QMetaType?
      These two methods still get the wrong type:
      QMetaType(qMetaTypeId<MyClassList>())
      QMetaType(qRegisterMetaType<MyClassList>("MyClassList"))

    Thanks in advance

    A 1 Reply Last reply
    0
    • crueeggC crueegg

      I'm migrating a Qt5.15 client/server to Qt6.5. QVariant::fromValue() returns a different type/name for aliased types. The appended sample code returns no longer the typedef name MyClassList in Qt6 with

      using MyClassList = QList<MyClass>;
      Q_DECLARE_METATYPE(MyClassList)
      

      Output:

      Qt 5.15.2
      QVariant::fromValue(MyClassList()).typeName=MyClassList
      QMetaType(qMetaTypeId<MyClassList>()).name=MyClassList
      
      Qt 6.5.0
      QVariant::fromValue(MyClassList()).typeName=QList<MyClass>
      QMetaType(qMetaTypeId<MyClassList>()).name=QList<MyClass>
      

      Code:

      #include <QCoreApplication>
      #include <QList>
      #include <QMetaProperty>
      #include <QVariant>
      
      class MyClass {};
      Q_DECLARE_METATYPE(MyClass);
      
      using MyClassList = QList<MyClass>;
      Q_DECLARE_METATYPE(MyClassList);
      
      int main(int argc, char *argv[])
      {
        QCoreApplication a(argc, argv);
        qInfo("Qt %s", QT_VERSION_STR);
      
        QVariant v = QVariant::fromValue(MyClassList());
        qInfo("QVariant::fromValue(MyClassList()).typeName=%s", v.typeName());
      
        QMetaType mtMcl = QMetaType(qMetaTypeId<MyClassList>());
        qInfo("QMetaType(qMetaTypeId<MyClassList>()).name=%s", qPrintable(mtMcl.name()));
      
        return 0;
      }
      
      • Is there a way QVariant uses the aliased name MyClassList?

      • How do I get the Aliased QMetaType?
        These two methods still get the wrong type:
        QMetaType(qMetaTypeId<MyClassList>())
        QMetaType(qRegisterMetaType<MyClassList>("MyClassList"))

      Thanks in advance

      A Offline
      A Offline
      Asperamanca
      wrote on last edited by
      #2

      @crueegg
      If memory serves, metatypes for any QList<RegisteredType> and similar are now automatically registered by Qt. I suspect that your Q_DECLARE_METATYPE is now simply ignored, since the type is already registered.

      crueeggC 1 Reply Last reply
      0
      • A Asperamanca

        @crueegg
        If memory serves, metatypes for any QList<RegisteredType> and similar are now automatically registered by Qt. I suspect that your Q_DECLARE_METATYPE is now simply ignored, since the type is already registered.

        crueeggC Offline
        crueeggC Offline
        crueegg
        wrote on last edited by
        #3

        @Asperamanca Q_DECLARE_METATYPE adds an additional QMetaType object with the aliased name to the global metasystem. But the compiler (clang) uses QList<MyClass> for T in QVariant::fromValue(), which results with the wrong QMetaType.

        A 1 Reply Last reply
        0
        • crueeggC crueegg

          @Asperamanca Q_DECLARE_METATYPE adds an additional QMetaType object with the aliased name to the global metasystem. But the compiler (clang) uses QList<MyClass> for T in QVariant::fromValue(), which results with the wrong QMetaType.

          A Offline
          A Offline
          Asperamanca
          wrote on last edited by
          #4

          @crueegg
          What exactly do you use the typename for?

          crueeggC 1 Reply Last reply
          0
          • A Asperamanca

            @crueegg
            What exactly do you use the typename for?

            crueeggC Offline
            crueeggC Offline
            crueegg
            wrote on last edited by
            #5

            @Asperamanca The customized typenames are used in the serialization. Qt5 client doesn't understand QList<MyClass> from the Qt6 server (vs). It only recognises the custom types (MyClassList).

            My prototype solution is to customize QVariant::save(QDataStream &s) where I map the typenames.

            I still didn't find a method where I can get the MyClassList QMetaType object. The debugger shows me the list with all registered names, but how can I iterate/access a specific one?

            A 1 Reply Last reply
            0
            • crueeggC crueegg

              @Asperamanca The customized typenames are used in the serialization. Qt5 client doesn't understand QList<MyClass> from the Qt6 server (vs). It only recognises the custom types (MyClassList).

              My prototype solution is to customize QVariant::save(QDataStream &s) where I map the typenames.

              I still didn't find a method where I can get the MyClassList QMetaType object. The debugger shows me the list with all registered names, but how can I iterate/access a specific one?

              A Offline
              A Offline
              Asperamanca
              wrote on last edited by
              #6

              @crueegg Maybe qRegisterMetaType can help.

              "This function is useful only for registering an alias (typedef) for every other use case Q_DECLARE_METATYPE and qMetaTypeId() should be used instead."

              crueeggC 1 Reply Last reply
              0
              • A Asperamanca

                @crueegg Maybe qRegisterMetaType can help.

                "This function is useful only for registering an alias (typedef) for every other use case Q_DECLARE_METATYPE and qMetaTypeId() should be used instead."

                crueeggC Offline
                crueeggC Offline
                crueegg
                wrote on last edited by crueegg
                #7

                @Asperamanca The qRegisterMetaTypeof the Q_DECLARE_METATYPE macro is implicitely called in QVariant::fromValue().
                I'm wondering when/how this alias is/can be retrieved.

                This is the only possible way I found, but I don't like to have strings as types:

                QMetaType mtFn = QMetaType::fromName("MyClassList");
                qInfo("QMetaType::fromName(\"MyClassList\").name=%s", qPrintable(mtFn.name()));
                

                Output: QMetaType::fromName("MyClassList").name=QList<MyClass>

                T 1 Reply Last reply
                0
                • crueeggC crueegg

                  @Asperamanca The qRegisterMetaTypeof the Q_DECLARE_METATYPE macro is implicitely called in QVariant::fromValue().
                  I'm wondering when/how this alias is/can be retrieved.

                  This is the only possible way I found, but I don't like to have strings as types:

                  QMetaType mtFn = QMetaType::fromName("MyClassList");
                  qInfo("QMetaType::fromName(\"MyClassList\").name=%s", qPrintable(mtFn.name()));
                  

                  Output: QMetaType::fromName("MyClassList").name=QList<MyClass>

                  T Offline
                  T Offline
                  T. Kloczko
                  wrote on last edited by
                  #8

                  @crueegg
                  Hi, unfortunately, we had to cope with the same issue. The only way to circumvent it was to create a singleton class that handles a mapping between the id of the QMetaType and the custom alias.
                  See https://gitlab.inria.fr/dtk/dtk-core/-/issues/30 for more details.
                  Thib.

                  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