Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Unsolved Connect() uses wrong emitter object.

    General and Desktop
    connect connection signal slot class
    5
    7
    1850
    Loading More Posts
    • 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.
    • K
      Kalado last edited by Kalado

      Hallo,
      I am having a problem with a connection and need some help.
      So I have one class A being utilized in another class B.
      I would like to connect a SIGNAL from A to a SLOT from B, within B.

      Look at some code:

      //A.h
      class task2 : public QWidget
      {
          Q_OBJECT
      public:
          task2(QWidget *parent = 0);
          ~task2();
      protected:
          void mousePressEvent(QMouseEvent *event) override;
      signals:
          void  myWidgetClicked();
      
      //A.cpp
      void A::mousePressEvent(QMouseEvent *event){
          emit myWidgetClicked();
      }
      

      I have a different connection in A using this SIGNAL within and without problems.

      B.h
      #include <A.h>
      QT_BEGIN_NAMESPACE
      class A;
      QT_END_NAMESPACE
      class B : public QMainWindow
      {
          Q_OBJECT
      private:
          A *a;
          void myFunction();
      private slots:
          void doSomething();
      
      //B.cpp
      B::B() : a (new A){
          myFunction();
      }
      
      void B:myFunction(){
          connect(a,SIGNAL(myWidgetClicked()),this,SLOT(doSomething()));
      }
      

      B is being shown in the main.cpp.

      I don't know why this doesn't work.

      Console says :

      QObject::connect: No such signal B::myWidgetClicked()
      

      But I entered "a" as the emitter.
      Any help would be much appreciated.

      Kind regards,
      Karim

      joeQ Pradeep Kumar 2 Replies Last reply Reply Quote 0
      • joeQ
        joeQ @Kalado last edited by

        @Kalado Hi,friend,welcome .

        You write wrong name of signal.

        signals:
            void  myWidgetClicked(); ///< this is your A's signal define
        

        But,

        void B:myFunction(){
            connect(a,SIGNAL(myWidgetC L icked()),this,SLOT(doSOmething())); ///< Notes the spelling error ‘L’ => 'l'
        }
        

        Just do it!

        1 Reply Last reply Reply Quote 0
        • K
          Kalado last edited by

          Hi joe,
          unfortunately this error is in this post only, the source code is without spelling errors.

          1 Reply Last reply Reply Quote 0
          • artwaw
            artwaw last edited by

            Have you tried to make slot public instead of private?

            For more information please re-read.

            Kind Regards,
            Artur

            1 Reply Last reply Reply Quote 0
            • T
              taedium last edited by

              @Kalado In principal it should work. Please paste your actual code, that at least compiles.

              1 Reply Last reply Reply Quote 0
              • Pradeep Kumar
                Pradeep Kumar last edited by

                Hi,

                @Kalado

                try the below code, to have a signal emitted from one class to another,

                "A.h"

                class A : public QWidget
                {
                    Q_OBJECT
                
                public:
                    A(QWidget *parent = 0);
                    ~A();
                
                
                    B *m_second;
                
                    void mousePressEvent(QMouseEvent *event) override;
                
                signals:
                    void  myWidgetClicked();
                };
                

                "A.cpp"

                A::A(QWidget *parent)
                    : QWidget(parent)
                {
                    m_second = new B;
                    connect(this,SIGNAL(myWidgetClicked()),m_second,SLOT(getEventSlot()));
                }
                
                void A::mousePressEvent(QMouseEvent *event)
                {
                    qDebug () << "mouse clicked :" << endl;
                    emit myWidgetClicked();
                }
                

                "B.h"

                class B : public QWidget
                {
                    Q_OBJECT
                public:
                    explicit B(QWidget *parent = 0);
                
                signals:
                
                public slots:
                    void getEventSlot();
                };
                

                "B.cpp"

                B::B(QWidget *parent) : QWidget(parent)
                {
                }
                
                void B::getEventSlot()
                {
                    qDebug () << Q_FUNC_INFO << "event came to slot of Class B on mouse clicked :" << endl;
                }
                

                Thanks,

                Pradeep Kumar
                Qt,QML Developer

                1 Reply Last reply Reply Quote 0
                • Pradeep Kumar
                  Pradeep Kumar @Kalado last edited by Pradeep Kumar

                  @Kalado said in Connect() uses wrong emitter object.:

                  Look at some code:

                  //A.h
                  class task2 : public QWidget
                  {
                      Q_OBJECT
                  public:
                      task2(QWidget *parent = 0);
                      ~task2();
                  protected:
                      void mousePressEvent(QMouseEvent *event) override;
                  signals:
                      void  myWidgetClicked();
                  
                  //A.cpp
                  void A::mousePressEvent(QMouseEvent *event){    /// use the respective classname
                      emit myWidgetClicked();
                  }
                  

                  If ur using Classname as task2 , use

                  void task2::mousePressEvent(QMouseEvent *event){
                      emit myWidgetClicked();
                   }
                  

                  Thanks,

                  Pradeep Kumar
                  Qt,QML Developer

                  1 Reply Last reply Reply Quote 0
                  • First post
                    Last post