[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!
6 replies
If you just want to store a custom type within a container (lst) 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;
- }
You must log in to post a reply. Not a member yet? Register here!

