Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Passing data between two forms
QtWS25 Last Chance

Passing data between two forms

Scheduled Pinned Locked Moved General and Desktop
10 Posts 2 Posters 2.2k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MBFernan
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • M Offline
        M Offline
        MBFernan
        wrote on last edited by
        #3

        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?

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @
          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;;
          

          }@

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • M Offline
            M Offline
            MBFernan
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              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

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • M Offline
                M Offline
                MBFernan
                wrote on last edited by
                #7

                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

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  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.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    MBFernan
                    wrote on last edited by
                    #9

                    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

                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

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

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved