How to inherit QObject based abstract class in two QWidget/QGraphicsScene views



  • I have a custom MVC classes and I want the views to either inherit QWidget or QGraphicsScene. Both should however also inherit from abstract view class that automatically handles the connection to the underlying model and provides several virtual slots to be implemented in an actual view class.

    However there seems to be no obvious way to do it because both QWidget/QGraphicsScene inherits QObject and my abstract view class must inherit QObject as well to be able to handle signals and slots.

    I think I can only duplicate the code in two view classes that inherit the QWidget and QGraphicsScene. Or is there any other way?


  • Qt Champions 2016

    Hi
    virtual inheritance will not work ?
    https://en.wikipedia.org/wiki/Virtual_inheritance

    Never tried it with Qt though so I have no idea with moc / signal / slot.


  • Moderators

    Hi,

    @Resurr3ction said:

    I want the views to either inherit QWidget or QGraphicsScene. Both should however also inherit from abstract view class

    QAbstractItemView already inherits QWidget, so if you inherit QAbstractItemView you already get all of QWidget's methods.

    QGraphicsScene is a "data store", not a visual object. Your view should not inherit QGraphicsScene. Use composition, not inheritance.

    However there seems to be no obvious way to do it because both QWidget/QGraphicsScene inherits QObject and my abstract view class must inherit QObject as well to be able to handle signals and slots.

    If you inherit QWidget, then your custom class is also a QObject so it can use signals and slots.

    @mrjj said:

    virtual inheritance will not work ?
    https://en.wikipedia.org/wiki/Virtual_inheritance

    Never tried it with Qt though so I have no idea with moc / signal / slot.

    Unfortunately, that won't work. You currently can't inherit from multiple QObjects.


  • Qt Champions 2016

    @JKSH
    Ah, moc assumes first class only. So its simply not supported.
    Thats good to know.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.