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. Connect with derived class
Forum Updated to NodeBB v4.3 + New Features

Connect with derived class

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 5 Posters 3.1k 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.
  • N Offline
    N Offline
    nefarious
    wrote on last edited by A Former User
    #1

    I have a base class:

    class QCcmStatusPanel  : public QFrame
    {
        Q_OBJECT
    public:
        explicit QCcmStatusPanel(QWidget *parent = 0);
        ~QCcmStatusPanel();
    
    private:
        QOverflowCtrl* mOverflowCtrl;
    
    public slots:
        void OverflowSlot(int overflow);
    };
    

    and a derived class

    class QCcmTimeDomainStatusPanel
            : public QCcmStatusPanel
    {
    public:
        QCcmTimeDomainStatusPanel(QWidget *parent = 0);
        ~QCcmTimeDomainStatusPanel();
    
    private:
        QDistributorDepthCtrl* mDistributorDepthCtrl;
    
    public slots:
        void DistributorDepthSlot(int distributorDepth);
    };
    

    in my application I instantiate the class as

        mStatusPanel = new QCcmTimeDomainStatusPanel(this);
        connect(distributor, SIGNAL(SetDepthSignal(int)),
                mStatusPanel, SLOT(DistributorDepthSlot(int)));
    

    and I get a message
    QObject::connect: No such slot QCcmStatusPanel::DistributorDepthSlot(int)
    complaining that the base class doesn't have the slot that is defined in the base class.

    Is there something I have to do in connect for this to work?

    jsulmJ 1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #2

      Hi! Whenever you use signals and slots in a class you need to add the Q_OBJECT.

      1 Reply Last reply
      5
      • N nefarious

        I have a base class:

        class QCcmStatusPanel  : public QFrame
        {
            Q_OBJECT
        public:
            explicit QCcmStatusPanel(QWidget *parent = 0);
            ~QCcmStatusPanel();
        
        private:
            QOverflowCtrl* mOverflowCtrl;
        
        public slots:
            void OverflowSlot(int overflow);
        };
        

        and a derived class

        class QCcmTimeDomainStatusPanel
                : public QCcmStatusPanel
        {
        public:
            QCcmTimeDomainStatusPanel(QWidget *parent = 0);
            ~QCcmTimeDomainStatusPanel();
        
        private:
            QDistributorDepthCtrl* mDistributorDepthCtrl;
        
        public slots:
            void DistributorDepthSlot(int distributorDepth);
        };
        

        in my application I instantiate the class as

            mStatusPanel = new QCcmTimeDomainStatusPanel(this);
            connect(distributor, SIGNAL(SetDepthSignal(int)),
                    mStatusPanel, SLOT(DistributorDepthSlot(int)));
        

        and I get a message
        QObject::connect: No such slot QCcmStatusPanel::DistributorDepthSlot(int)
        complaining that the base class doesn't have the slot that is defined in the base class.

        Is there something I have to do in connect for this to work?

        jsulmJ Online
        jsulmJ Online
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #3

        @nefarious mStatusPanel is a QCcmStatusPanel, right? Since QCcmStatusPanel does not have DistributorDepthSlot slot you get that error. You need to cast the pointer mStatusPanel to QCcmTimeDomainStatusPanel* in the connect call (http://doc.qt.io/qt-5.7/qobject.html#qobject_cast).

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

        1 Reply Last reply
        1
        • BuckwheatB Offline
          BuckwheatB Offline
          Buckwheat
          wrote on last edited by
          #4

          @jsulm where are you seeing the code for your comment??? I agree with @Wieland that the Q_OBJECT needs to be in QCcmTimeDomainStatusPanel.

          Dave Fileccia

          jsulmJ 1 Reply Last reply
          0
          • BuckwheatB Buckwheat

            @jsulm where are you seeing the code for your comment??? I agree with @Wieland that the Q_OBJECT needs to be in QCcmTimeDomainStatusPanel.

            jsulmJ Online
            jsulmJ Online
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @Buckwheat In @nefarious first post

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

            1 Reply Last reply
            1
            • mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Hi
              If i put his code in a .h file I get
              "Error: Class declaration lacks Q_OBJECT macro."
              for QCcmTimeDomainStatusPanel
              (mingw compiler)

              1 Reply Last reply
              0
              • BuckwheatB Offline
                BuckwheatB Offline
                Buckwheat
                wrote on last edited by
                #7

                @jsulm ... I still don't see the declaration for mStatusPanel. I think it is being assumed in the argument. I am not saying you are wrong on the intended declaration but I still believe the problem is the missing Q_OBJECT.

                Dave Fileccia

                jsulmJ 1 Reply Last reply
                0
                • BuckwheatB Buckwheat

                  @jsulm ... I still don't see the declaration for mStatusPanel. I think it is being assumed in the argument. I am not saying you are wrong on the intended declaration but I still believe the problem is the missing Q_OBJECT.

                  jsulmJ Online
                  jsulmJ Online
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @Buckwheat My assumption is based on the error message:

                  QObject::connect: No such slot QCcmStatusPanel::DistributorDepthSlot(int)
                  

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

                  1 Reply Last reply
                  0
                  • BuckwheatB Offline
                    BuckwheatB Offline
                    Buckwheat
                    wrote on last edited by
                    #9

                    @jsulm ... so was mine and @Wieland. But I use SIGNAL()/SLOT() (older method) with base classes all the time without issue unless I forget my Q_OBJECT or Q_GADGET.

                    Dave Fileccia

                    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