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_OBJECT

    public:
    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_OBJECT

    public:
    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&#40;&#41;;
    

    }
    @

    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&#40;&#41;;
    

    }@

    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


  • Lifetime Qt Champion

    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?


  • Lifetime Qt Champion

    @
    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&#40;&#41;;
    

    }@



  • 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&#40;&#41;;
    

    }@

    And nothing happens.

    Do you think it's something with the conducao.exec()? Should it be exec or open? I'm not sure


  • Lifetime Qt Champion

    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


  • Lifetime Qt Champion

    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&#40;&#41;;
    

    }
    @

    However you should really consider first looking at Qt's documentation examples and tutorials. It will give you the basics to build on.



  • Thank you so much SGaist.

    Actually I always read the documentation, but it doesn't help me a lot.

    Is it possible to pass my variable "a" if it's an array instead of a single number?

    Best regards,
    Marcos


  • Lifetime Qt Champion

    Yes it is possible. Following my last code sample you can just change a in both method for whatever you want.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.