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::type with QString

QMetaType::type with QString

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 3.4k 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.
  • G Offline
    G Offline
    goocreations
    wrote on last edited by
    #1

    I want to return the id of a previously registered custom class by using a QString. I've tried the following, but all of them return QMetaType::UnknownType

    @QString name = "MyClass";

    QMetaType::type(name.toLatin1().data()); // fail
    QMetaType::type(name.toUtf8().data()); // fail
    QMetaType::type(name.toLocal8Bit() .data()); // fail

    char *chars = new char[name.length()];
    strcpy(chars, name.toStdString().c_str());
    QMetaType::type(chars); // fail@

    I thought there was a bug with QMetaType or Q_DECLARE_METATYPE. This is not the case, since the following succeeded:

    @QMetaType::type("MyClass"); // success@

    Since no QString object is passed, the parameter is (I assume) directly used as a char*. Does anyone know how to correctly decode a QString to a char* so that QMetaType::type works correctly?

    EDIT: Does this maybe have something to do with the terminating-character?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Two questions for you:

      • Did you use qRegisterMetaType<>() ?
      • What combination of Qt/Platform are you using ?

      QMetaType::type will only work if your type has been registered.

      Hope it helps

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • C Offline
        C Offline
        ChrisW67
        wrote on last edited by
        #3

        Works fine here:
        @
        #include <QCoreApplication>
        #include <QDebug>

        class MyClass { };
        Q_DECLARE_METATYPE(MyClass)

        int main(int argc, char **argv)
        {
        QCoreApplication app(argc, argv);

        int type = qRegisterMetaType<MyClass>("MyClass");
        qDebug() << "MyClass" << type;
        
        QString name("MyClass");
        qDebug() << "QString" << QMetaType::type(name.toAscii());
        qDebug() << "String literal" << QMetaType::type("MyClass");
        
        MyClass *c =  static_cast<MyClass*>(QMetaType::construct(type));
        qDebug() << c;
        delete c;
        
        return 0;
        

        }
        @

        I suspect you have not called qRegisterMetaType<>().

        1 Reply Last reply
        0
        • G Offline
          G Offline
          goocreations
          wrote on last edited by
          #4

          I'm using Qt5 and qRegisterMetaType is not documented anymore, so I'm not sure if it's deprecated. I also don't want to use qRegisterMetaType, since it is run-time bound. Q_DECLARE_METATYPE on the other hand is (as far as I know) preprocessor bound.

          I've registered my class with:

          @Q_DECLARE_METATYPE(MyClass);@

          and since

          @QMetaType::type("MyClass");@

          worked and I was able to create an object with this, means that the registration was successful. My problem is with retrieving it by using a QString. The toAscii() function is also gone in the new Qt5, so I'm not sure if toLatin1() does exactly the same, but for some reason the passed char* is not working.

          1 Reply Last reply
          0
          • G Offline
            G Offline
            goocreations
            wrote on last edited by
            #5

            Ok, I'm not sure what I did, but can't replicate my problem. So I don't think it has anything to do with the QString encoding. Here is my class decleration:

            @class MyClass
            {
            ...
            };

            Q_DECLARE_METATYPE(MyClass);

            int main()
            {
            if(QMetaType::type("MyClass") == QMetaType::UnknownType)
            {
            int id = qMetaTypeId<ViSigmoidActivationFunction>();
            cout << id <<endl;
            cout << QMetaType::typeName(id) << endl;
            }
            }@

            The output of main is as follows:

            @1126
            MyClass@

            So my registration of my class was successfull, but there is a problem retireving the id using QMetaType::type("MyClass"). When I add the following to the first line of main:

            @qRegisterMetaType<MyClass>("MyClass");@

            the 2 output staments are never execute, meaning my type was found. But I don't want to use this, since it is a runtime registration. Additionally Qt5 always refers to Q_DECLARE_METATYPE, saying that qRegisterMetaType is only when you want to use signals/slots parameters.

            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