Passing data between two forms
-
Hello everybody, I'm trying to do something very simple. Pass data from one form to another.
When I push a button, the variable "a" assumes a value (22) and I want to show this value in the Label of the other form (conducao).
conducao.h
@#ifndef CONDUCAO_H
#define CONDUCAO_H#include <QDialog>
namespace Ui {
class Conducao;
}class Conducao : public QDialog
{
Q_OBJECTpublic:
explicit Conducao(QWidget *parent = 0);
~Conducao();public slots:
void onInfoPassed(double Info_Trem);private:
Ui::Conducao *ui;};
#endif // CONDUCAO_H
@principal.h
@#ifndef PRINCIPAL_H
#define PRINCIPAL_H#include <QDialog>
#include <QTableView>
#include <QItemDelegate>
#include <QStandardItemModel>
#include <QVector>namespace Ui {
class Principal;
}class Principal : public QDialog
{
Q_OBJECTpublic:
explicit Principal(QWidget *parent = 0);
~Principal();private slots:
void on_pushButton_clicked();private:
Ui::Principal *ui;
QStandardItemModel *model;signals:
void Info_Collected(double Info_Trem);};
#endif // PRINCIPAL_H@
conducao.cpp
@#include "conducao.h"
#include "ui_conducao.h"
#include "QString"Conducao::Conducao(QWidget *parent) :
QDialog(parent),
ui(new Ui::Conducao)
{
ui->setupUi(this);
}Conducao::~Conducao()
{
delete ui;
}void Conducao::onInfoPassed(double Info_Trem)
{
QString a;
a.setNum(Info_Trem);
ui->label->setText("a");
}
@main.cpp
@#include "principal.h"
#include <QApplication>
#include "conducao.h"int main(int argc, char *argv[])
{QApplication a(argc, argv); Principal w1; Conducao w2; QObject::connect(&w1, SIGNAL(Info_Collected(double Info_Trem)), &w2, SLOT(onInfoPassed(double Info_Trem))); w1.show(); return a.exec();
}
@principal.cpp
@#include "principal.h"
#include "ui_principal.h"
#include "Lista_de_Locomotivas.h"
#include "conducao.h"Principal::Principal(QWidget *parent) :
QDialog(parent),
ui(new Ui::Principal)
{
ui->setupUi(this);connect(ui->pushButton, SIGNAL(clicked()),this, SLOT(on_pushButton_clicked()));
}
Principal::~Principal()
{
delete ui;
}void Principal::on_pushButton_clicked()
{double a = 22; emit Info_Collected(a); this->hide(); Conducao conducao; conducao.setModal(true); conducao.exec();
}@
When I run it, Qt says:
QObject::connect: No such signal Principal::Info_Collected(double Info_Trem) in D:\Projeto Nova Condu??o Padr?o\CPDR\CPDR\main.cpp:13
QObject::connect: (sender name: 'Principal')
QObject::connect: (receiver name: 'Conducao')I really don't know what is happening and I appreciate if anybody can help, it's kind urgent for me.
Regards, Marcos -
Hi,
@
QObject::connect(&w1, SIGNAL(Info_Collected(double Info_Trem)),
&w2, SLOT(onInfoPassed(double Info_Trem)));
@is wrong, you don't pass the parameter name to either SIGNAL or SLOT.
@
QObject::connect(&w1, SIGNAL(Info_Collected(double)),
&w2, SLOT(onInfoPassed(double)));
@Should to the trick
-
Hello SGaist,
Thank you, but it still doesnt work. Now the error is not showing anymore. But seems like the void:
@void Conducao::onInfoPassed(double Info_Trem)
{
QString a;
a.setNum(22);
ui->label->setText("a");
}@Is not being "achievid". It opens the new window but the textLabel doenst change. Do you have any idea why?
-
@
void Principal::on_pushButton_clicked()
{double a = 22; emit Info_Collected(a); this->hide(); Conducao conducao; << this is not the same you have in your main.cpp conducao.setModal(true); conducao.exec();
}@
-
What you mean?
I changed to:@#include "principal.h"
#include <QApplication>
#include "conducao.h"int main(int argc, char *argv[])
{QApplication a(argc, argv); Principal w1; Conducao conducao; QObject::connect(&w1, SIGNAL(Info_Collected(double)), &conducao, SLOT(onInfoPassed(double))); w1.show(); return a.exec();
}@
And nothing happens.
Do you think it's something with the conducao.exec()? Should it be exec or open? I'm not sure
-
I mean that in on_pushButton_clicked you are creating a new instance of Conducao that has nothing to do with the one your have in your main.cpp.
It's the one you have in your main.cpp that receives the update. Not the one you are creating in on_pushButton_clicked
-
OK. Now I understand.
I'm trying to make some modifications here. But it's not going well.What should I do/change?Should I write something in my main.cpp?
I'm trying to do this:
@void Conducao::onInfoPassed(double Info_Trem)
{Conducao conducao; conducao.setModal(true); //conducao.open(); conducao.show(); ui->label->setText("a");
}@
But nothing happens with the label...
I appreciate your help and attention SGaist. It's making more clear to me
-
No, now you are creating a new instance of your class inside onInfoPassed.
Since you want to call it from on_pushButton_clicked and to simplify things:
@
void Principal::on_pushButton_clicked()
{double a = 22; Conducao conducao; conducao.onInfoPassed(a); conducao.setModal(true); conducao.exec();
}
@However you should really consider first looking at Qt's documentation examples and tutorials. It will give you the basics to build on.
-
Yes it is possible. Following my last code sample you can just change a in both method for whatever you want.