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. Overriding a protected pure virtual function with a signal
Forum Updated to NodeBB v4.3 + New Features

Overriding a protected pure virtual function with a signal

Scheduled Pinned Locked Moved Unsolved General and Desktop
19 Posts 7 Posters 3.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.
  • M Offline
    M Offline
    mooglus
    wrote on last edited by
    #1

    Is there any problem with overriding a protected pure virtual function with a signal in Qt?
    The following seems to work...

    CppAbstractClass
    {
      protected:
        virtual void NotifyChangeToSubClass(int)=0;
      private:
        void DoTheThings() { NotifyChangeToSubClass(42); }
    };
    
    class MyQtClass : public QObject, public CppAbstractClass
    {
      public:
        MyQtClass::MyQtClasss() {
          connect(this, SIGNAL(NotifyChangeToSubClass(int)), this, SLOT(MySlot(int)));
        }
      private slots:
        void MySlot(int Val) { qDebug() << Val; };
    
      signals:
        void NotifyChangeToSubClass(int) override;
    };
    

    I wondered just because I always thought of Qt signals as public. Moc is fleshing out the implementation of NotifyChangeToSubClass like any other signal.

    jsulmJ 1 Reply Last reply
    0
    • M mooglus

      Is there any problem with overriding a protected pure virtual function with a signal in Qt?
      The following seems to work...

      CppAbstractClass
      {
        protected:
          virtual void NotifyChangeToSubClass(int)=0;
        private:
          void DoTheThings() { NotifyChangeToSubClass(42); }
      };
      
      class MyQtClass : public QObject, public CppAbstractClass
      {
        public:
          MyQtClass::MyQtClasss() {
            connect(this, SIGNAL(NotifyChangeToSubClass(int)), this, SLOT(MySlot(int)));
          }
        private slots:
          void MySlot(int Val) { qDebug() << Val; };
      
        signals:
          void NotifyChangeToSubClass(int) override;
      };
      

      I wondered just because I always thought of Qt signals as public. Moc is fleshing out the implementation of NotifyChangeToSubClass like any other signal.

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

      @mooglus Why don't you simply define NotifyChangeToSubClass as signal in CppAbstractClass? Why do you want to override it? There is no point in overriding signals, as code for signals is generated - you can't modify it's behaviour.

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

      M 1 Reply Last reply
      3
      • M Offline
        M Offline
        mooglus
        wrote on last edited by
        #3
        This post is deleted!
        1 Reply Last reply
        0
        • jsulmJ jsulm

          @mooglus Why don't you simply define NotifyChangeToSubClass as signal in CppAbstractClass? Why do you want to override it? There is no point in overriding signals, as code for signals is generated - you can't modify it's behaviour.

          M Offline
          M Offline
          mooglus
          wrote on last edited by
          #4

          @jsulm Sorry, I wasn't clear, the CppAbstractClass is a common base class for code that isn't Qt

          1 Reply Last reply
          0
          • Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @mooglus said in Overriding a protected pure virtual function with a signal:

            base class for code that isn't Qt

            Why do you need a signal signature in there then?

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            M 1 Reply Last reply
            0
            • Christian EhrlicherC Christian Ehrlicher

              @mooglus said in Overriding a protected pure virtual function with a signal:

              base class for code that isn't Qt

              Why do you need a signal signature in there then?

              M Offline
              M Offline
              mooglus
              wrote on last edited by mooglus
              #6

              @Christian-Ehrlicher My base class calls a virtual function which is meant to be overridden by platform specific code. That code could be Qt, or objective c++, or whatever. In the case of Qt, rather than overriding the virtual function to then just emit another signal, I wanted to make that override the actual signal.

              JonBJ 1 Reply Last reply
              0
              • M mooglus

                @Christian-Ehrlicher My base class calls a virtual function which is meant to be overridden by platform specific code. That code could be Qt, or objective c++, or whatever. In the case of Qt, rather than overriding the virtual function to then just emit another signal, I wanted to make that override the actual signal.

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by JonB
                #7

                @mooglus said in Overriding a protected pure virtual function with a signal:

                rather than overriding the virtual function to then just emit another signal, I wanted to make that override the actual signal.

                You know what you're going to be suggested/told! Safer to leave it as a normal method, like the other code expects, and do whatever you want Qt-wise, like emitting a signal, inside that method :) You may be fine with what you're doing, but for the sake of one extra function call it doesn't seem worth it.

                On a separate matter/bug-bear. You look like you're a reasonable programmer/Qt-er. So why are you still using the unhelpful, old-style SIGNAL/SLOT() macros when you could & should be using the new style ones, which at least have compile-time support? :)

                M 1 Reply Last reply
                1
                • JonBJ JonB

                  @mooglus said in Overriding a protected pure virtual function with a signal:

                  rather than overriding the virtual function to then just emit another signal, I wanted to make that override the actual signal.

                  You know what you're going to be suggested/told! Safer to leave it as a normal method, like the other code expects, and do whatever you want Qt-wise, like emitting a signal, inside that method :) You may be fine with what you're doing, but for the sake of one extra function call it doesn't seem worth it.

                  On a separate matter/bug-bear. You look like you're a reasonable programmer/Qt-er. So why are you still using the unhelpful, old-style SIGNAL/SLOT() macros when you could & should be using the new style ones, which at least have compile-time support? :)

                  M Offline
                  M Offline
                  mooglus
                  wrote on last edited by
                  #8

                  @JonB It's undoubtedly safer, but in larger classes these extra functions can add up to lots of boiler plate.
                  Well spotted, on the old-school SIGNAL/SLOT() :D We have lots of legacy code that hasn't been had these calls weeded out. I did notice as I was pasting the example, but thought it didn't matter too much for this example.

                  JonBJ 1 Reply Last reply
                  0
                  • M mooglus

                    @JonB It's undoubtedly safer, but in larger classes these extra functions can add up to lots of boiler plate.
                    Well spotted, on the old-school SIGNAL/SLOT() :D We have lots of legacy code that hasn't been had these calls weeded out. I did notice as I was pasting the example, but thought it didn't matter too much for this example.

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by
                    #9

                    @mooglus said in Overriding a protected pure virtual function with a signal:

                    but in larger classes these extra functions can add up to lots of boiler plate.

                    Nah, I'm talking about a one-liner. You can even add it in-line in the .h file. If you're not prepared to add one line of code to an interface between two different products/systems you're in trouble. Even I would do it, and believe me I like minimal code! Totally my opinion/your choice of course. I'll leave others to answer your specific approach.

                    1 Reply Last reply
                    2
                    • fcarneyF Offline
                      fcarneyF Offline
                      fcarney
                      wrote on last edited by
                      #10

                      Multiple inheritance with QObject could be problematic. When combining functionality with QObject classes I usually use composition rather than inheritance. I have run into issues with being able to copy objects based upon QObject. It also eliminates the use of templates for those classes. If I need a signal on an object that is not QObject based, and I can't or don't want to inherit from QObject. I will often create a subclass that gets created in the constructor and have it call methods on the parent object. I call this object a signal mule.

                      C++ is a perfectly valid school of magic.

                      M 1 Reply Last reply
                      1
                      • fcarneyF fcarney

                        Multiple inheritance with QObject could be problematic. When combining functionality with QObject classes I usually use composition rather than inheritance. I have run into issues with being able to copy objects based upon QObject. It also eliminates the use of templates for those classes. If I need a signal on an object that is not QObject based, and I can't or don't want to inherit from QObject. I will often create a subclass that gets created in the constructor and have it call methods on the parent object. I call this object a signal mule.

                        M Offline
                        M Offline
                        mooglus
                        wrote on last edited by
                        #11

                        @fcarney Interesting reply. I've not run into trouble (yet) with multiple inheritance and Qt. Wouldn't you need to define an interface for your mule? That sounds like a bit of a hassle.

                        JonBJ 1 Reply Last reply
                        0
                        • fcarneyF Offline
                          fcarneyF Offline
                          fcarney
                          wrote on last edited by
                          #12

                          @mooglus said in Overriding a protected pure virtual function with a signal:

                          Wouldn't you need to define an interface for your mule?

                          How would that be bad? If I need functionality I code that functionality.

                          C++ is a perfectly valid school of magic.

                          M 1 Reply Last reply
                          0
                          • fcarneyF fcarney

                            @mooglus said in Overriding a protected pure virtual function with a signal:

                            Wouldn't you need to define an interface for your mule?

                            How would that be bad? If I need functionality I code that functionality.

                            M Offline
                            M Offline
                            mooglus
                            wrote on last edited by
                            #13

                            @fcarney It just seems like more typing to provide functionality that inheritance provides. However, if you're in a situation where inheritance is causing you problems then it makes sense. I've read many times that it's good to favour composition over inheritance, in practice I often find it inconvenient. I would upvote you, but your reputation is 666, I could never ruin that ;)

                            fcarneyF Q 2 Replies Last reply
                            1
                            • M mooglus

                              @fcarney It just seems like more typing to provide functionality that inheritance provides. However, if you're in a situation where inheritance is causing you problems then it makes sense. I've read many times that it's good to favour composition over inheritance, in practice I often find it inconvenient. I would upvote you, but your reputation is 666, I could never ruin that ;)

                              fcarneyF Offline
                              fcarneyF Offline
                              fcarney
                              wrote on last edited by
                              #14

                              @mooglus said in Overriding a protected pure virtual function with a signal:

                              but your reputation is 666

                              Makes sense. I use dark mode Qt Creator, makes me a haxor!

                              C++ is a perfectly valid school of magic.

                              1 Reply Last reply
                              2
                              • M mooglus

                                @fcarney Interesting reply. I've not run into trouble (yet) with multiple inheritance and Qt. Wouldn't you need to define an interface for your mule? That sounds like a bit of a hassle.

                                JonBJ Offline
                                JonBJ Offline
                                JonB
                                wrote on last edited by
                                #15

                                @mooglus said in Overriding a protected pure virtual function with a signal:

                                I've not run into trouble (yet) with multiple inheritance and Qt.

                                If you do mean from QObject (perhaps you don't) then that is surprising. Inheriting from more than one QObject-derived class is problematic. There are various versions of Qt here, maybe things have changed over the years.

                                https://stackoverflow.com/questions/8578657/qobject-multiple-inheritance/8578921
                                https://www.ics.com/blog/multiple-inheritance-qt
                                https://forum.qt.io/topic/88295/with-qt-5-6-is-it-possible-to-avoid-the-diamond-problem-when-signals-and-slots-are-defined-in-two-branches

                                https://doc.qt.io/qt-5/moc.html#multiple-inheritance-requires-qobject-to-be-first

                                If you are using multiple inheritance, moc assumes that the first inherited class is a subclass of QObject. Also, be sure that only the first inherited class is a QObject.

                                1 Reply Last reply
                                0
                                • fcarneyF Offline
                                  fcarneyF Offline
                                  fcarney
                                  wrote on last edited by
                                  #16

                                  I wanted to inherit a templated class with a QObject. I ran into issues. Also multiple QObject inheritance doesn't work either.

                                  C++ is a perfectly valid school of magic.

                                  Christian EhrlicherC 1 Reply Last reply
                                  1
                                  • fcarneyF fcarney

                                    I wanted to inherit a templated class with a QObject. I ran into issues. Also multiple QObject inheritance doesn't work either.

                                    Christian EhrlicherC Offline
                                    Christian EhrlicherC Offline
                                    Christian Ehrlicher
                                    Lifetime Qt Champion
                                    wrote on last edited by
                                    #17

                                    @fcarney said in Overriding a protected pure virtual function with a signal:

                                    I wanted to inherit a templated class with a QObject.

                                    Works fine as long as you don't need moc stuff in the template

                                    I ran into issues. Also multiple QObject inheritance doesn't work either.

                                    Can not work to the parent-child relatonsship

                                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                                    Visit the Qt Academy at https://academy.qt.io/catalog

                                    kshegunovK 1 Reply Last reply
                                    3
                                    • Christian EhrlicherC Christian Ehrlicher

                                      @fcarney said in Overriding a protected pure virtual function with a signal:

                                      I wanted to inherit a templated class with a QObject.

                                      Works fine as long as you don't need moc stuff in the template

                                      I ran into issues. Also multiple QObject inheritance doesn't work either.

                                      Can not work to the parent-child relatonsship

                                      kshegunovK Offline
                                      kshegunovK Offline
                                      kshegunov
                                      Moderators
                                      wrote on last edited by
                                      #18

                                      @Christian-Ehrlicher said in Overriding a protected pure virtual function with a signal:

                                      Can not work to the parent-child relatonsship

                                      Nor moc, more specifically it confuses the hell out of the meta-object system (e.g QMetaObject).

                                      Read and abide by the Qt Code of Conduct

                                      1 Reply Last reply
                                      0
                                      • M mooglus

                                        @fcarney It just seems like more typing to provide functionality that inheritance provides. However, if you're in a situation where inheritance is causing you problems then it makes sense. I've read many times that it's good to favour composition over inheritance, in practice I often find it inconvenient. I would upvote you, but your reputation is 666, I could never ruin that ;)

                                        Q Offline
                                        Q Offline
                                        QtTester
                                        wrote on last edited by QtTester
                                        #19
                                        This post is deleted!
                                        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