QMetaType::type with QString



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


  • Lifetime Qt Champion

    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



  • 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<>().



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



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.