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. Inheriting interface with signals
Qt 6.11 is out! See what's new in the release blog

Inheriting interface with signals

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 7.2k 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.
  • G Offline
    G Offline
    Gloweye
    wrote on last edited by
    #1

    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 ?

    jsulmJ 1 Reply Last reply
    0
    • G Gloweye

      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 ?

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

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

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      G 1 Reply Last reply
      2
      • A Offline
        A Offline
        Asperamanca
        wrote on last edited by
        #3

        This article might be helpful:
        https://stackoverflow.com/questions/17943496/declare-abstract-signal-in-interface-class

        1 Reply Last reply
        2
        • jsulmJ jsulm

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

          G Offline
          G Offline
          Gloweye
          wrote on last edited by
          #4

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

          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