Unable to connect signal to slot in another class
Asked Answered
I

2

7

I have 2 classes. Class A and Class B. I am emitting a signal from class A which I want the B to recieve.

I am doing it following way

In Listener File

Header File:
  Class Listener:public DDSDataReaderListener
  {
     //Some code
    public:
       A m_objectSendData;
  }

Implementation File:

  void Listener::ondataavailable(DDSDataReader *reader)
 {
  m_objSendData.GetDDSData();
 }

In Class A

Header File:

Class A:public QObject
{
  Q_OBJECT
  public:
    void GetDDSData();
  signals:
    void Signal_Data();
}

.cpp File

 A::A(QWidget *parent):QObject(parent)
{
}

void A::GetDDSData()
 {
   emit Signal_Data();
 }   

In Class B

Header File:

  Class B:public QObject
  {
    Q_Object
    public:
    A objGetData;

    public slots:
    void getData();
  }

Implementation File:

 B::B(QWidget *parent):QObject(parent)
{
   //Some part of code

  connect(&objGetData,SIGNAL(Signal_Data()),this,SLOT(getData());
 }

 void B::getData()
 {
    //Watever is to be updated
  }

I tried debugging. It is going till emit part correctly. However it is not reaching the slot. Can someone please help me with this. Thank You.

Installation answered 7/8, 2013 at 7:15 Comment(6)
Do you get any messages like Object::connect: No such slot B::SlotB() when you run your application?Hekking
No it is not showing any such messageInstallation
Well I can't see anything wrong in the code you posted above, except for the missing ) before ; in your connect statement, but that obviously isn't missing in your actual code.Hekking
check boolean value connect returns. If it is true then connection was successful and you just don't emit signal. If it returns false then check answer that @Merlin069 has gave you.Superficies
One more thing, you didn't show enough code but I suspecting that you program leaves scope of objectA variable and your emitting object is just destroyed before it can emit any signal (objectA is local variable created on stack not on heap).Superficies
possible duplicate of C++ Qt signal and slot not firingFinical
L
16

Without full code, it's quite difficult to identify the exact issue of the problem, so I'll outline a few important points to check.

To ensure you can use the signal and slots mechanism, you should ensure that your class is derived, from QObject or a class already derived from QObject in its hierarchy and your class must contain the Q_OBJECT macro, for example: -

class A : public QObject // derived from QObject
{
    Q_OBJECT // your class must have this macro for signals and slots

    public:
    A();
};

Omitting the macro is probably the most common of mistakes.

To specify a slot, you add it to either the public or private slot section of your class: -

class B : public QObject // derived from QObject
{
    Q_OBJECT // your class must have this macro for signals and slots

    public:
    B();

    public slots:
        void SlotB(); // slot declared public

    private slots:
        void SlotBPrivate(); // slot declared private.
}; 

Once a signal is declared in a class, a slot to receive the signal should match the arguments passed in and when you connect a signal to a slot, you must not add the function argument names.

Therefore: -

connect(&objectA, SIGNAL(SignalA(int in), this, SIGNAL(SlotA(int param)); //will fail due to the argument names

It should be: -

connect(&objectA, SIGNAL(SignalA(int), this, SIGNAL(SlotA(int));

Finally, if you're using Qt 5, you can use the new connection call, which doesn't require you to specify any argument, but instead takes the addresses of slot and signal functions.

connect(&objectA, &A::SignalA, this, &B::SlotA));

Since it references the address of a function, in actuality, the functions don't need to be classed as a slot and will still be called.

Hope that helps.

Loam answered 7/8, 2013 at 7:42 Comment(5)
I have done exactly the same way. I am still not understanding what the problem is. my function and slot both are public.Installation
Then can you please add more code to the question, such as the header files of where you declare the slots and signals. Also note that when you run your application with a debugger and a connect call fails, an error is displayed in the output window. Please post that too.Loam
I will explain you the entire scenario what I am doing. I am having a DDS listener from where I am calling the QT function. From this function I am emitting a signal which goes to my Main thread slot. Here I update the values coming from DDS. I have included all the header files and there is not much to write in code. It is exactly as I have written. Only addition I can make is in my listener I am creating an object of class A and calling the function. That is the only thing I am doing in Listener. And while debugging it is saying Stopped at breakpoint three which is at the connect statement.Installation
I'm sorry, but while you say you've included all your code, you clearly haven't as there is no definition of the class, no Q_OBJECT macro, no signals or slot labels etc. While you may assume you've provided all the necessary information, it's not enough to go on here. Rather than your question stating, for example, "In class A...", you need to show the code for us to understand its context and assist you with the problem you're having. I could show you the code "a = 4" and tell you that it's crashing, but without you knowing what 'a' is, and its surrounding code you have no clue why.Loam
I think you have several mistakes in your examples: you show SIGNAL() twice instead of SIGNAL() and SLOT(); also the number of parenthesis opened and closed to not match. This is definitely why Qt5 signals are much better (in my opinion) since any kind of error is more than likely going to be caught at compile time.Corby
N
1

Actually I believe an answer is given in one of the comments.

One more thing, you didn't show enough code but I suspecting that you program leaves scope of objectA variable and your emitting object is just destroyed before it can emit any signal (objectA is local variable created on stack not on heap). – Marek R 1 hour ago

you allocate your Object on stack, so it gets destroyed as soon as it gets out of scope, together with destroy it gets disconnected from all signals/slots it has connections to.

So that's why you don't see any errors/warning messages because code itself is completely legit. You should new() your object to get it allocated in heap.

Nawrocki answered 7/8, 2013 at 9:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.