May 4, 2012

Maxim Prishchepa Maxim Prishchepa
Lab Rat
96 posts

[SOLVED] multiple using qRegisterMetaType

 

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

  1. class MyClass {
  2.    public: MyClass(){
  3.       qRegisterMetaType<MyClass>("MyClass");
  4.    }
  5. }
  6.  
  7. ...
  8. for(int i = 0; i < 10; ++i){
  9.    MyClass * cs = new MyClass();
  10.    lst.append(cs);
  11. }

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!

 Signature 

Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz).

6 replies

May 4, 2012

Lukas Geyer Lukas Geyer
Gene Splicer
2074 posts

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.

  1. // library.cpp
  2.  
  3. namespace
  4. {
  5.     class LibraryInitializationObject
  6.     {
  7.     public:
  8.         LibraryInitializationObject()
  9.         {
  10.             qRegisterMetaType<MyClass>("MyClass");
  11.             ...
  12.         }
  13.     };
  14.  
  15.     LibraryInitializationObject libraryInitializationObject;
  16. }

May 4, 2012

Maxim Prishchepa Maxim Prishchepa
Lab Rat
96 posts

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

 Signature 

Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz).

May 4, 2012

Lukas Geyer Lukas Geyer
Gene Splicer
2074 posts

If you just want to store your class in a QHash you do not need to declare or register a metatype.

  1. QHash<T, MyClass*> lst;

May 4, 2012

Maxim Prishchepa Maxim Prishchepa
Lab Rat
96 posts

realy?
I try to do that, have an errors:

  1. class MyClass{
  2.  
  3. };
  4. class TcpServer : public QTcpServer { Q_OBJECT
  5. //-------------------------------------------------------------
  6. 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’*

 Signature 

Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz).

May 4, 2012

Maxim Prishchepa Maxim Prishchepa
Lab Rat
96 posts

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!

 Signature 

Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz).

May 6, 2012

Lukas Geyer Lukas Geyer
Gene Splicer
2074 posts

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

 
  ‹‹ Display problem with QCheckBox      QDialog Embedded in QWidget ››

You must log in to post a reply. Not a member yet? Register here!