Kushan Peiris
Kushan Peiris

Reputation: 167

Signal and slot wrong value sending(Qt c++)

I have written a small program to send data from one form(MainWindow) to another(Dialog) upon a button click. When the button is clicked the value written in the lineEdit of MainWindow is to be displayed on a label in Dialog form!

When I click the button a value is displayed on the label but it is not the same as the value entered in the line edit! following are the respective codes in the 2 header and 2 cpp files!

MainWindow.h

class MainWindow : public QMainWindow
{
   Q_OBJECT

   signals:
   void sendIntData(int data);
   public:
   explicit MainWindow(QWidget *parent = 0);
   ~MainWindow();
}

MainWIndow.cpp

void MainWindow::on_pushButton_clicked()
{
   Dialog *dialog1=new Dialog(this);

   dialog1->setModal(true);
   dialog1->exec();

   int o=ui->lineEdit->text().toInt();


   connect(this, SIGNAL(sendIntData(int)),dialog1, SLOT(setIntData(int)));


   emit sendIntData(o);

}

Dialog.h

class Dialog : public QDialog
{
   Q_OBJECT

   public slots:
   void setIntData(int data);

   public:
   explicit Dialog(QWidget *parent = 0);
   ~Dialog();
}

Dialog.cpp

Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::DIalog)
{
   ui->setupUi(this);
   QString value=QString::number(index);
   ui->label->setText(value);
}

Dialog::~Dialog()
{
   delete ui;
}
void Dialog::setIntData(int data)
{
   index=data;
}

eg-When I click 3 and press the button I get a value 7237481! How can I correct this?

Upvotes: 1

Views: 408

Answers (3)

Farhad
Farhad

Reputation: 4181

I think you are showing int value which not initialized.

emit signal:

int o=ui->lineEdit->text().toInt();
connect(this, SIGNAL(sendIntData(int)),dialog1, SLOT(setIntData(int)));
emit sendIntData(o);

Show value:

void Dialog::setIntData(int data)
{
   ui->label->setText(QString::number(data));
}

Upvotes: 0

saeed
saeed

Reputation: 2497

Replace connect and emit in on_pushButton_clicked()

void MainWindow::on_pushButton_clicked()
{
   Dialog *dialog1=new Dialog(this);

   dialog1->setModal(true);
   dialog1->exec();

   int o=ui->lineEdit->text().toInt();

   connect(this, SIGNAL(sendIntData(int)),dialog1, SLOT(setIntData(int)));

   emit sendIntData(o);
}

Upvotes: 2

Tazo leladze
Tazo leladze

Reputation: 1489

If only once we convey our dialogue, the importance of signal and slot is not necessary. It is possible to give this value to the constructor or to do the initialize function and to give it the values.

//way 1:
void MainWindow::on_pushButton_clicked(){
    Dialog *dlg = new Dialog();
    connect(this, SIGNAL(SendData(int)), dlg, SLOT(slotData(int)));
    emit SendData(ui->lineEdit->text().toInt());
    dlg->exec();
}

void Dialog::slotData(int arg1)
{
    ui->label->setText(QString::number(arg1));
}

//way 2:
void MainWindow::on_pushButton_clicked(){
    Dialog* dlg = new Dialog(ui->lineEdit->text().toInt());
    dlg->exec();
}

//way 3:
#include "dialog.h"
#include "ui_dialog.h"
#include "QDebug"

Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
{
    ui->setupUi(this);
}

Dialog::~Dialog()
{
    delete ui;
}

void Dialog::initialize(int value)
{
     ui->label->setText(QString::number(value));
}


void MainWindow::on_pushButton_clicked(){
    Dialog *dlg = new Dialog();
    dlg->initialize(ui->lineEdit->text().toInt());
    dlg->exec();
}

Upvotes: 1

Related Questions