Inheriting interface with signals



  • For a personal project, I am building a Minesweeper app.

    I found myself in the need to build 2 implementations of the grid(which contains the nodes) and the nodes(which are the fields that might be mines). I have the same issue with both of them, but I'll focus on the Nodes here, as any solution might be portable.

    One implementation of the Nodes should be visible. It inherits from QWidget, and emits a signal stateChanged. This is connected to a slot which handles game over, game won, and things like that.

    However, I also want an implementation that does not have to be rendered. The interface is exactly the same, including it's signal stateChanged.

    My experience from Python says that I need an abstract base class, which takes out the common functionality. I know that using base class pointers, I could use either inheriting class the very same way. However, I found out the hard way that you cannot diamond-inherit QObject. So now i have code like the following:

    //Of course, this is in three files
    
    class AbstractNode {
    // stuff is defined, including pure virtual functions
    signals:
        void stateChanged(State state);
    }
    Q_DECLARE_INTERFACE(AbstractNode, NodeInterface)
    
    class Node: public QWidget, public AbstractNode {
    
    // specific definitions (actually, just implementation and interface declaration)
    }
    
    class AINode: public QObject, public AbstractNode {
    //specific definitions
    }
    

    My question is, can I define the signal this way ? Are there any nasty surprises down the road by inheriting the signal declaration into a QObject subclass ? And will slots work as well ?


  • Moderators

    @Gloweye Class AbstractNode should be derived from QObject and have Q_OBJECT macro if it has any signals/slots.





  • @jsulm Problem with that is, that means I can't inherit it with the Node class, since that one requires QWidget. That gives Diamond inheritance, which QObject can't handle.

    @Asperamanca Thanks, this looks promising. Cant believe I couldn't find that question.


Log in to reply
 

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