Tan Viet
Tan Viet

Reputation: 2113

Cannot receive data when using Signal & Slot in QT

I have 2 forms which is created by QT Creator. I have used Signal & Slot to transfer data between those 2 forms. But I can't receive any data.

And these are 2 forms in my application

Form1.h

class Form1: public QDialog
{

...........

private slots:
    void on_btnOK_clicked();

signals:
    void SendId(int id);
};

Form1.cpp

#include "form2.h"

void Form1::on_btnOK_clicked()
{
   emit SendId(2);            //ID = 2

   Form2 form2;
   form2.setModal(true);
   form2.exec();
}

Form2.h

class Form2 : public QDialog
{

...........

public slots:
   void ReceiveId(int id);
private:
   Form1* m_pForm1;
};

Form2.cpp

Form2::Form2(QWidget *parent) :
QDialog(parent),
ui(new Ui::Form2)

{
   ui->setupUi(this);
   m_pForm1 = new Form1(this);

    // Connecting the signal we created in the Form1
    // with the slot created in the Form2
    QObject::connect(m_pForm1, SIGNAL(SendId(int)),
                     this, SLOT(ReceiveId(int)));
}

void Form2::ReceiveId(int id)
{
    qDebug() << "Received id";
}

When I run the application, I don't see the message "Received id". Is my application wrong?

Do you have any ideas?

Thanks!

Upvotes: 0

Views: 961

Answers (3)

SpongeBobFan
SpongeBobFan

Reputation: 964

I think you're doing it wrong.
You're creating new Form1 object in Form2's constructor, and it seems like user will newer see it and no methods will be called so no signals is emitted.

When you want to send data between forms using signals, it should look like this:
You have Form1 and Form2 objects, both are created and likely both are visible to user at the same time.
Then you call connect() to connect Form1's signal to Form2's slot
Then you emit signal in Form1, so Form2's slot is called and data is transmitted

If your forms is not intended to be on the screen simultaneously, just send data when you're creating Form2 in Form1 without signals and slots.

Upvotes: 1

s4eed
s4eed

Reputation: 7901

You created form 2 after emitting the signal. and the pointer in m_pForm1 is completely new Form and is useless. change your code like this :

Form1::Form1()//change your constructor depending on your need
{
   m_pForm2 = new Form2;
   form2->setModal(true);
   connect(this, SIGNAL(sendId(int)), m_pForm2, SLOT(receiveId(int))); 
}

void Form1::on_btnOK_clicked()
{
   m_pForm2->show();
   emit SendId(2);            //ID = 2

}

Upvotes: 1

Daniel Castro
Daniel Castro

Reputation: 1290

I guess it's because the Form1 instance you're listening to is never raising a signal.

Suppose you start creating an instance of Form1, show it and click on the OK button. Then, this instance of Form1 raises a signal, but no one is listening yet:

void Form1::on_btnOK_clicked()
{
   emit SendId(2); // No one is listening yet

   Form2 form2;
   form2.setModal(true);
   form2.exec();
}

A Form2 instance is then created and shown, which inside creates another Form1 instance. Form2 connects to the new Form1 SendId signal, but this signal is never emitted because this new Form1 is never shown:

Form2::Form2(QWidget *parent) :
QDialog(parent),
ui(new Ui::Form2)

{
   ui->setupUi(this);
   m_pForm1 = new Form1(this);  // You're creating a new Form1 instance, but you never show it
    QObject::connect(m_pForm1, SIGNAL(SendId(int)),
                     this, SLOT(ReceiveId(int))); // SendId signal of the new Form1 is never raised
}

Upvotes: 1

Related Questions