Results 1 to 5 of 5

Thread: QObject inherittance problems

  1. #1
    Join Date
    Feb 2006
    Location
    USA
    Posts
    142
    Thanks
    24
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default QObject inherittance problems

    I'm just having trouble coming to grips with this issue. At the moment, I'm trying to figure out how to implement a widget base class that can be sub-classed by either a QWidget-based class or a QGLWidget-based class. I can't figure out how to implement such a structure . Here's the problem:

    Qt Code:
    1. .
    2. +-----------------+
    3. | Abstract Base |
    4. | Class |
    5. +-----------------+
    6. / \
    7. / \
    8. / \
    9. / \
    10. +-----------------+ +------------------+
    11. | QWidget-based | | QGLWidget-based |
    12. | class | | class |
    13. +-----------------+ +------------------+
    To copy to clipboard, switch view to plain text mode 
    The abstract base class would include signals and slots that its sub-classes would implement. This creates a problem. If I were to say that the abstract base class inheritted QWidget, I can make the QWidget subclass work, but not the QGLWidget. If I were to say that the abstract base class inheritted QGLWidget, I can make the QGLWidget subclass work, but not the QWidget subclass. If I were to simply make the abstract base class inherit QObject, neither would work. Any idea how to resolve such a thing?

    I can't get rid of all reference to a QObject ancestor because I require the virtual signals and slots inherittance.
    Life without passion is death in disguise

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QObject inherittance problems

    In general you can't do it this way. In this situation the base class can't inherit QObject. Maybe instead of inheritance you should use some other association? For example a has-a relationship? Could you explain what you're trying to do?

  3. The following user says thank you to wysota for this useful post:

    KShots (24th April 2007)

  4. #3
    Join Date
    Feb 2006
    Location
    USA
    Posts
    142
    Thanks
    24
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: QObject inherittance problems

    basically, what I'm trying to do is define an interface that a bunch of plugins should follow. Each plugin should implement a series of slots. If they fail to do so, they need to produce a compile-time error. Theoretically, I could do this by not inheritting the base class at all and implementing the slots in the individual plugins, but if a plugin fails to implement a slot, it will still compile and run with weird results.

    Here's the structure of what I've got:
    Qt Code:
    1. .
    2. +------------------+ +--------------------------------+
    3. | Plugin Interface | | Abstract base class with slots |
    4. +------------------+ +--------------------------------+
    5. | |
    6. +------------------+ +------------------------------+
    7. | Plugins (a,b...) |---------| QWidget/QGLWidget sub-class |
    8. +------------------+ +------------------------------+
    To copy to clipboard, switch view to plain text mode 
    The section on the right is a simplified version of my previous diagram. My plugin has a "has a" relationship with the QWidget / QGLWidget class, which it would return on request (effectively bypassing the plugin itself requiring QWidget, and theoretically allowing the QWidget to have abstract slots defined that all plugins should follow).
    Life without passion is death in disguise

  5. #4
    Join Date
    Feb 2006
    Location
    USA
    Posts
    142
    Thanks
    24
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: QObject inherittance problems

    Ok, I think I can get away with this if I seperate things one more step further:
    Qt Code:
    1. .
    2. +-----------+ +-------------------+ +------------------+
    3. | Plugin | | Abstract class | | Abstract base |
    4. | Interface | | knows about slots | | class with slots |
    5. +-----------+ +-------------------+ +------------------+
    6. | | |
    7. +---------+ +-------------------+ +----------------+
    8. | Plugins |----| QWidget/QGLWidget |-| plugin slots |
    9. | (a,b...)| | sub-class | | implementation |
    10. +---------+ +-------------------+ +----------------+
    To copy to clipboard, switch view to plain text mode 
    This way, my plugin can pass a QWidget, which would have an implementation class of slots. I think the nature of my slots would allow such a relationship. It may get tricky, depending on the plugin, as far as gaining access to the QWidget itself (perhaps a pointer to the QWidget sub-class in the slots implementation?), but I think it's doable. The "abstract class that knows about slots" does not inherit QObject or similar, but it knows about the abstract base class with slots (and includes it as a pointer variable).

    EDIT: This structure compiles. I think I have a solution. Thanks for the idea .
    Last edited by KShots; 24th April 2007 at 16:14.
    Life without passion is death in disguise

  6. #5
    Join Date
    Aug 2006
    Posts
    44
    Thanked 7 Times in 7 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QObject inherittance problems

    I'm going to skip drawing the ASCII art here. :P

    But, I've done something similar using a template as my base class. It may hamper usage of signals and slots, though -- haven't tested that.

    The 'leaf' classes inherit the template, which has two arguments: the QWidget-based class and the plugin interface:

    template<class QW, class PI>
    MyMixer
    {
    };

    then

    class MyWidgetWithCoolStuff : public MyMixer<QWidget, PlugIn>
    {
    Q_OBJECT
    };

    ...

    class MyOtherWidgetWithCoolStuff : public MyMixer<QGLWidget, PlugIn>
    {
    Q_OBJECT
    };

    As I said... I haven't had the need to use signals / slots with this, so I wouldn't be surprised of moc has trouble with it. In my case, I have several separate "mixin" interfaces that one can pair with a QWidget-based window, and the template implements all the common code.

    Fun! Fun! FUN!!!

Similar Threads

  1. Reparenting a QObject
    By ghorwin in forum Qt Programming
    Replies: 1
    Last Post: 13th April 2007, 17:21
  2. Replies: 2
    Last Post: 8th March 2007, 22:22
  3. Interface composition and QObject
    By brcain in forum Qt Programming
    Replies: 9
    Last Post: 20th November 2006, 17:56
  4. QT4 Plugins - problems, problems
    By NormanDunbar in forum Qt Programming
    Replies: 6
    Last Post: 9th May 2006, 15:39

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.