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. [SOLVED] multiple using qRegisterMetaType
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] multiple using qRegisterMetaType

Scheduled Pinned Locked Moved General and Desktop
7 Posts 2 Posters 6.8k Views 1 Watching
  • 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.
  • M Offline
    M Offline
    maxim.prishchepa
    wrote on last edited by
    #1

    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!

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

    1 Reply Last reply
    0
    • L Offline
      L Offline
      lgeyer
      wrote on last edited by
      #2

      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;
      

      }
      @

      1 Reply Last reply
      0
      • M Offline
        M Offline
        maxim.prishchepa
        wrote on last edited by
        #3

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

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

        1 Reply Last reply
        0
        • L Offline
          L Offline
          lgeyer
          wrote on last edited by
          #4

          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;
          @

          1 Reply Last reply
          0
          • M Offline
            M Offline
            maxim.prishchepa
            wrote on last edited by
            #5

            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'

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

            1 Reply Last reply
            0
            • M Offline
              M Offline
              maxim.prishchepa
              wrote on last edited by
              #6

              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!

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

              1 Reply Last reply
              0
              • L Offline
                L Offline
                lgeyer
                wrote on last edited by
                #7

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

                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