[SOLVED] multiple using qRegisterMetaType



  • Hi All!
    Can i use qRegisterMetaType at the constructor of my clss, for example:

    @class MyClass {
    public: MyClass(){
    qRegisterMetaType<MyClass>("MyClass");
    }
    }

    ...
    for(int i = 0; i < 10; ++i){
    MyClass * cs = new MyClass();
    lst.append(cs);
    }@

    is this a mistake or correct code?

    ps: i can't write qRegisterMetaType at the main function or at some singleton class, i write a lib.

    tnx 4 answer's!



  • If you just want to store a custom type within a container (<code>lst</code>) there is no need to declare or register the custom type.

    You'll need Q_DECLARE_METATYPE() if you want to store your type within a QVariant and you will additionally need qRegisterMetaType<>() if you want to dynamically create and destroy objects of that type at runtime, mostly for queued signal and slots connections or the QObject property system.

    However, as you need the type to be registered to create it, I don't think putting qRegisterMetaType<>() into the types constructor is going to work out.

    The cleanest solution would most probably be an initialize() method of your library, which is called to setup the library. A global initialization object might be another solution, as long as you make sure that you don't run into initialization order problems.
    @
    // library.cpp

    namespace
    {
    class LibraryInitializationObject
    {
    public:
    LibraryInitializationObject()
    {
    qRegisterMetaType<MyClass>("MyClass");
    ...
    }
    };

    LibraryInitializationObject libraryInitializationObject;
    

    }
    @



  • tnx 4 answer! i need push my class to the QHash table, thats why i try to solve this issue.



  • If you just want to store your class in a QHash you do not need to declare or register a metatype.
    @
    QHash<T, MyClass*> lst;
    @



  • realy?
    I try to do that, have an errors:
    @class MyClass{

    };
    class TcpServer : public QTcpServer { Q_OBJECT
    //-------------------------------------------------------------
    private: QHash<MyClass *> test;@

    have errors: error C2976: 'QHash' : too few template arguments
    1> d:\projects\devtools\qt\4.8.0\include\qtcore\qhash.h(259) : see declaration of 'QHash'



  • oh... I'm sorry!! forget write second arg at the template))

    I remember why i try to registrate my class: my class don't have a default constructiors, thats why QHash can't add that class to the container...

    Tnx 4 anwsers! i think this topic is SOLVED!



  • Well, you can't register classes having no public default constructor either ;-)


Log in to reply
 

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