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. Using QMetaType to get the QMetaObject instance for a private, forward declared class?
Forum Updated to NodeBB v4.3 + New Features

Using QMetaType to get the QMetaObject instance for a private, forward declared class?

Scheduled Pinned Locked Moved Solved General and Desktop
qmetaobjectqmetatypeqobject
4 Posts 3 Posters 2.4k Views 2 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.
  • G Offline
    G Offline
    Guy Gizmo
    wrote on 6 Feb 2016, 23:40 last edited by Guy Gizmo 2 Jun 2016, 23:41
    #1

    I have two QObject derived classes, one of which is public and one which is private. The private one contains platform specific code and I'm sequestering it to its source file using the Pimpl idiom. I'd like to use both of these classes with Qt's property system and meta type system, so that I can both have properties on other classes that are pointers to these classes and so that I can access their QMetaObjects via QMetaType.

    So basically, my header file looks like this:

    class MyClass_p;
    
    class MyClass : public QObject
    {
    	Q_OBJECT
    public:
    	Q_INVOKABLE MyClass(QObject *parent = NULL);
    	...
    	MyClass_p *impl;
    	Q_PROPERTY(MyClass_p *impl MEMBER impl)
    };
    
    Q_DECLARE_OPAQUE_POINTER(MyClass_p*);
    Q_DECLARE_METATYPE(MyClass_p*);
    

    And my source file looks like this:

    class MyClass_p : public QObject {
    	Q_OBJECT
    public:
    	Q_INVOKABLE MyClass_p(QObject *parent = NULL);
    	...
    };
    
    // implementation of MyClass's and MyClass_p's methods go here
    
    #include "MyClass.moc"
    

    When I access the QMetaObject for MyClass like this:

    QMetaType(QMetaType::type("MyClass*")).metaObject();
    

    ...it works and I get the correct QMetaClass instance. But when I do the same thing for MyClass_p...

    QMetaType(QMetaType::type("MyClass_p*")).metaObject();
    

    I get NULL, as though it's not a class that's derived from QObject. But it is.

    I've tried calling qRegisterMetaType< MyClass_p*>() shortly after the application starts, but I still get NULL when I try to retrieve its QMetaObject.

    Can anyone help me figure out what I'm doing wrong? Is it even possible to use the QMetaType / QMetaObject system with a private class?

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 7 Feb 2016, 15:00 last edited by
      #2

      hi
      do you have
      class MyClass_p : public QObject
      in the cpp only?
      moc.exe is only run on .h files as far as I know so it would not see it.

      1 Reply Last reply
      0
      • C Online
        C Online
        Chris Kawa
        Lifetime Qt Champion
        wrote on 7 Feb 2016, 20:33 last edited by
        #3

        If it's a private implementation of a QObject then it really shouldn't be a QObject itself.
        If the class is to be used by other classes it should not really be private. It kinda defeats the purpose of being private.

        1 Reply Last reply
        0
        • G Offline
          G Offline
          Guy Gizmo
          wrote on 7 Feb 2016, 21:33 last edited by
          #4

          @mrjj I believe you are correct. I found out that the problem is my use of Q_DECLARE_OPAQUE_POINTER. It apparently prevents the meta type from registering as being derived from a QObject, which makes sense since at that point there's no way for Qt to know what the class is.

          I refactored my code so that MyClass_p is no longer private or forward declared, and it fixed the issue.

          1 Reply Last reply
          0

          2/4

          7 Feb 2016, 15:00

          • Login

          • Login or register to search.
          2 out of 4
          • First post
            2/4
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved