Connect() uses wrong emitter object.
Unsolved
General and Desktop
-
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 -
@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' }
-
Hi,
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,
-
@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,