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. Multiple inheritance and signals/slots

Multiple inheritance and signals/slots

Scheduled Pinned Locked Moved Solved General and Desktop
13 Posts 4 Posters 2.1k Views 3 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.
  • AndyBriceA Offline
    AndyBriceA Offline
    AndyBrice
    wrote on last edited by
    #1

    I understood multiple inheritance is ok, as long as only one of the base classes is derived from QObject and this class is inherited first. But I can't get it to work.

    My code looks like this:

    class DelayedEditSpinBox : public QSpinBox
    {
    Q_OBJECT
    
    protected slots:
    
        void onValueChanged();
       ...
    };
    
    DelayedEditSpinBox::DelayedEditSpinBox( QWidget* parent )
    : QSpinBox( parent )
    {
        connect( this, SIGNAL( valueChanged( int ) ), this, SLOT( onValueChanged() ) );
    }
    
    class ParameterWidget
    {
    public:
    
        ParameterWidget( Parameter* parameter );
    
        virtual ~ParameterWidget();
        ...
    };
    
    class IntParameterWidget : public DelayedEditSpinBox, public ParameterWidget
    {
    Q_OBJECT
    
    public:
    
        explicit IntParameterWidget( QWidget* parent, Parameter* parameter );
        ...
    };
    

    The issue is that in the DelayedEditSpinBox::onValueChanged() slot never receives the DelayedEditSpinBox::valueChanged( int ) signal when DelayedEditSpinBox is derived from IntParameterWidget . But it does if DelayedEditSpinBox is 'standalone'.

    Everything works fine if IntParameterWidget is derived from QSpinBox instead of DelayedEditSpinBox and I connect to the SpinBox::valueChanged( int ) signal.

    Any ideas?

    Pl45m4P 1 Reply Last reply
    0
    • AndyBriceA AndyBrice

      I understood multiple inheritance is ok, as long as only one of the base classes is derived from QObject and this class is inherited first. But I can't get it to work.

      My code looks like this:

      class DelayedEditSpinBox : public QSpinBox
      {
      Q_OBJECT
      
      protected slots:
      
          void onValueChanged();
         ...
      };
      
      DelayedEditSpinBox::DelayedEditSpinBox( QWidget* parent )
      : QSpinBox( parent )
      {
          connect( this, SIGNAL( valueChanged( int ) ), this, SLOT( onValueChanged() ) );
      }
      
      class ParameterWidget
      {
      public:
      
          ParameterWidget( Parameter* parameter );
      
          virtual ~ParameterWidget();
          ...
      };
      
      class IntParameterWidget : public DelayedEditSpinBox, public ParameterWidget
      {
      Q_OBJECT
      
      public:
      
          explicit IntParameterWidget( QWidget* parent, Parameter* parameter );
          ...
      };
      

      The issue is that in the DelayedEditSpinBox::onValueChanged() slot never receives the DelayedEditSpinBox::valueChanged( int ) signal when DelayedEditSpinBox is derived from IntParameterWidget . But it does if DelayedEditSpinBox is 'standalone'.

      Everything works fine if IntParameterWidget is derived from QSpinBox instead of DelayedEditSpinBox and I connect to the SpinBox::valueChanged( int ) signal.

      Any ideas?

      Pl45m4P Offline
      Pl45m4P Offline
      Pl45m4
      wrote on last edited by Pl45m4
      #2

      @AndyBrice said in Multiple inheritance and signals/slots:

      connect( this, SIGNAL( valueChanged( int ) ), this, SLOT( onValueChanged() ) );

      Idk if just a typo, but the slot has to accept intvalues, if signal passes an integer.

      Nevermind. Was confused because of parameter differences :)
      (e.g. Int + String wont work)

      Is your class DelayedEditSpinBox pure virtual or do you actually create instances?


      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      AndyBriceA 1 Reply Last reply
      0
      • Pl45m4P Pl45m4

        @AndyBrice said in Multiple inheritance and signals/slots:

        connect( this, SIGNAL( valueChanged( int ) ), this, SLOT( onValueChanged() ) );

        Idk if just a typo, but the slot has to accept intvalues, if signal passes an integer.

        Nevermind. Was confused because of parameter differences :)
        (e.g. Int + String wont work)

        Is your class DelayedEditSpinBox pure virtual or do you actually create instances?

        AndyBriceA Offline
        AndyBriceA Offline
        AndyBrice
        wrote on last edited by
        #3

        @Pl45m4 A slot can ignore the parameters in a signal. I do it all the time.

        J.HilkJ 1 Reply Last reply
        1
        • AndyBriceA AndyBrice

          @Pl45m4 A slot can ignore the parameters in a signal. I do it all the time.

          J.HilkJ Offline
          J.HilkJ Offline
          J.Hilk
          Moderators
          wrote on last edited by
          #4

          @AndyBrice said in Multiple inheritance and signals/slots:

          @Pl45m4 A slot can ignore the parameters in a signal. I do it all the time.

          That's indeed the case, however, I would suggest using the "new" Qt5 Signal Slot Syntax

          You can even specify the signal/slot to use, if to use it from the parent class or not

          DelayedEditSpinBox::DelayedEditSpinBox( QWidget* parent )
          : QSpinBox( parent )
          {
              connect( this, QOverLoad<int>::of(&QSpinBox::valueChanged), this, &DelayedEditSpinBox::onValueChanged);
          }
          

          in case you shadow signals or slots


          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          AndyBriceA 2 Replies Last reply
          5
          • VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by
            #5
            • What's the implementation of IntParameterWidget::IntParameterWidget?
            • Does DelayedEditSpinBox have multiple constructors?

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            AndyBriceA 1 Reply Last reply
            5
            • VRoninV VRonin
              • What's the implementation of IntParameterWidget::IntParameterWidget?
              • Does DelayedEditSpinBox have multiple constructors?
              AndyBriceA Offline
              AndyBriceA Offline
              AndyBrice
              wrote on last edited by
              #6

              @VRonin

              What's the implementation of IntParameterWidget::IntParameterWidget?

              IntParameterWidget::IntParameterWidget( QWidget* parent, Parameter* parameter )
              : DelayedEditSpinBox( parent ), ParameterWidget( parameter )
              {
              ...
              }
              

              It connects to a signal from DelayedEditSpinBox. But that signal is never emitted.

              Does DelayedEditSpinBox have multiple constructors?

              No,

              1 Reply Last reply
              0
              • J.HilkJ J.Hilk

                @AndyBrice said in Multiple inheritance and signals/slots:

                @Pl45m4 A slot can ignore the parameters in a signal. I do it all the time.

                That's indeed the case, however, I would suggest using the "new" Qt5 Signal Slot Syntax

                You can even specify the signal/slot to use, if to use it from the parent class or not

                DelayedEditSpinBox::DelayedEditSpinBox( QWidget* parent )
                : QSpinBox( parent )
                {
                    connect( this, QOverLoad<int>::of(&QSpinBox::valueChanged), this, &DelayedEditSpinBox::onValueChanged);
                }
                

                in case you shadow signals or slots

                AndyBriceA Offline
                AndyBriceA Offline
                AndyBrice
                wrote on last edited by
                #7

                @J-Hilk I will try that. Thanks!

                1 Reply Last reply
                0
                • J.HilkJ J.Hilk

                  @AndyBrice said in Multiple inheritance and signals/slots:

                  @Pl45m4 A slot can ignore the parameters in a signal. I do it all the time.

                  That's indeed the case, however, I would suggest using the "new" Qt5 Signal Slot Syntax

                  You can even specify the signal/slot to use, if to use it from the parent class or not

                  DelayedEditSpinBox::DelayedEditSpinBox( QWidget* parent )
                  : QSpinBox( parent )
                  {
                      connect( this, QOverLoad<int>::of(&QSpinBox::valueChanged), this, &DelayedEditSpinBox::onValueChanged);
                  }
                  

                  in case you shadow signals or slots

                  AndyBriceA Offline
                  AndyBriceA Offline
                  AndyBrice
                  wrote on last edited by
                  #8

                  @J-Hilk This syntax doesn't seem to work on MSVC. :0(

                  Pl45m4P 1 Reply Last reply
                  0
                  • AndyBriceA AndyBrice

                    @J-Hilk This syntax doesn't seem to work on MSVC. :0(

                    Pl45m4P Offline
                    Pl45m4P Offline
                    Pl45m4
                    wrote on last edited by
                    #9

                    @AndyBrice

                    Qt Version? Compiler Version?
                    In general it should work with MSVC
                    https://wiki.qt.io/New_Signal_Slot_Syntax


                    If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                    ~E. W. Dijkstra

                    AndyBriceA 1 Reply Last reply
                    2
                    • Pl45m4P Pl45m4

                      @AndyBrice

                      Qt Version? Compiler Version?
                      In general it should work with MSVC
                      https://wiki.qt.io/New_Signal_Slot_Syntax

                      AndyBriceA Offline
                      AndyBriceA Offline
                      AndyBrice
                      wrote on last edited by
                      #10

                      @Pl45m4 MSVC 2015 on Windows 7.

                      1 Reply Last reply
                      0
                      • VRoninV Offline
                        VRoninV Offline
                        VRonin
                        wrote on last edited by
                        #11

                        I tested the below with Qt 5.12.5, MinGW 7.3.0 64bit on Win 10 and it's working

                        #include <QSpinBox>
                        #include <QApplication>
                        class Parameter;
                        class DelayedEditSpinBox : public QSpinBox
                        {
                        public:
                            explicit DelayedEditSpinBox( QWidget* parent =nullptr )
                                : QSpinBox( parent )
                                {
                                    connect( this,static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged), this, &DelayedEditSpinBox::onValueChanged );
                                }
                            void onValueChanged(){
                                qDebug("onValueChanged");
                            }
                        };
                        
                        class ParameterWidget
                        {
                        public:
                            ParameterWidget( Parameter* parameter =nullptr ){}
                            virtual ~ParameterWidget()=default;
                        };
                        
                        class IntParameterWidget : public DelayedEditSpinBox, public ParameterWidget
                        {
                        
                        public:
                        
                            explicit IntParameterWidget( QWidget* parent=nullptr, Parameter* parameter=nullptr )
                                :DelayedEditSpinBox(parent),ParameterWidget(parameter)
                            {}
                        
                        };
                        
                        int main(int argc, char *argv[])
                        {
                            QApplication a(argc, argv);
                            IntParameterWidget w;
                            w.show();
                            return a.exec();
                        }
                        

                        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                        ~Napoleon Bonaparte

                        On a crusade to banish setIndexWidget() from the holy land of Qt

                        AndyBriceA 1 Reply Last reply
                        4
                        • VRoninV VRonin

                          I tested the below with Qt 5.12.5, MinGW 7.3.0 64bit on Win 10 and it's working

                          #include <QSpinBox>
                          #include <QApplication>
                          class Parameter;
                          class DelayedEditSpinBox : public QSpinBox
                          {
                          public:
                              explicit DelayedEditSpinBox( QWidget* parent =nullptr )
                                  : QSpinBox( parent )
                                  {
                                      connect( this,static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged), this, &DelayedEditSpinBox::onValueChanged );
                                  }
                              void onValueChanged(){
                                  qDebug("onValueChanged");
                              }
                          };
                          
                          class ParameterWidget
                          {
                          public:
                              ParameterWidget( Parameter* parameter =nullptr ){}
                              virtual ~ParameterWidget()=default;
                          };
                          
                          class IntParameterWidget : public DelayedEditSpinBox, public ParameterWidget
                          {
                          
                          public:
                          
                              explicit IntParameterWidget( QWidget* parent=nullptr, Parameter* parameter=nullptr )
                                  :DelayedEditSpinBox(parent),ParameterWidget(parameter)
                              {}
                          
                          };
                          
                          int main(int argc, char *argv[])
                          {
                              QApplication a(argc, argv);
                              IntParameterWidget w;
                              w.show();
                              return a.exec();
                          }
                          
                          AndyBriceA Offline
                          AndyBriceA Offline
                          AndyBrice
                          wrote on last edited by
                          #12

                          @VRonin I will try it, thanks. It might be a while, because I am going to be away from my dev PC.

                          1 Reply Last reply
                          0
                          • AndyBriceA Offline
                            AndyBriceA Offline
                            AndyBrice
                            wrote on last edited by
                            #13

                            It turns out that I was later calling QObject::disconnect() on IntParameterWidget, which also disconnected signals in the base classes, including the QComboBox valueChanged( int ) signal. So it isn't a Qt bug/issue. Just my stupidity. Oops.

                            1 Reply Last reply
                            3

                            • Login

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • Users
                            • Groups
                            • Search
                            • Get Qt Extensions
                            • Unsolved