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() uses wrong emitter object.
QtWS25 Last Chance

Connect() uses wrong emitter object.

Scheduled Pinned Locked Moved Unsolved General and Desktop
connectconnectionsignalslotclass
7 Posts 5 Posters 2.6k Views
  • 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 Offline
    K Offline
    Kalado
    wrote on last edited by Kalado
    #1

    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

    joeQJ Pradeep KumarP 2 Replies Last reply
    0
    • K 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

      joeQJ Offline
      joeQJ Offline
      joeQ
      wrote on last edited by
      #2

      @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
      0
      • K Offline
        K Offline
        Kalado
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • artwawA Offline
          artwawA Offline
          artwaw
          wrote on last edited by
          #4

          Have you tried to make slot public instead of private?

          For more information please re-read.

          Kind Regards,
          Artur

          1 Reply Last reply
          0
          • T Offline
            T Offline
            taedium
            wrote on last edited by
            #5

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

            1 Reply Last reply
            0
            • Pradeep KumarP Offline
              Pradeep KumarP Offline
              Pradeep Kumar
              wrote on last edited by
              #6

              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
              0
              • K 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

                Pradeep KumarP Offline
                Pradeep KumarP Offline
                Pradeep Kumar
                wrote on last edited by Pradeep Kumar
                #7

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

                • Login

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