Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved communicate data between two windows

    General and Desktop
    6
    19
    665
    Loading More Posts
    • 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.
    • Albator
      Albator last edited by

      Hello qt people,

      I have two mainwindows.ui and in the first windows I make a 2+2 calculation and I display this result in my first window in a qlabel and I would like to be able to display this result in my second window! how do I do it?

      //seconde_p2.cpp
      #include "seconde_p2.h"
      
      #include "mainwindow.h"
      
      #include "ui_seconde_p2.h"
      
      
      seconde_p2::seconde_p2(QWidget *parent) :
          QMainWindow(parent),
          ui(new Ui::seconde_p2)
      {
          ui->setupUi(this);
          
          int a,b,c;
          a=2;
          b=2;
          c=a+b;
          ui->lineEdit->setText(QString::number(c)); 
      
      
      }
      
      seconde_p2::~seconde_p2()
      {
          delete ui;
      }
      
      //seconde_p3.cpp
      #include "seconde_p3.h"
      #include "seconde_p2.h"
      
      #include "ui_seconde_p3.h"
      
      
      seconde_p3::seconde_p3(QWidget *parent) :
          QMainWindow(parent),
          ui(new Ui::seconde_p3)
      {
          ui->setupUi(this);
          
           // how use c here 
      }
      
      seconde_p3::~seconde_p3()
      {
          delete ui;
      }
      
      1 Reply Last reply Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        Hi,

        You can use Signals and Slots, or a simple setter, or pass the value as a parameter of the constructor.

        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 Reply Quote 2
        • mrjj
          mrjj Lifetime Qt Champion last edited by

          Hi
          One way is to give it to the window when you create it.
          Change its constructor to accept extra parameter
          and give it to it when constructed.

          int c= xxxx
          seconde_p3 * win3 = new seconde_p3(this, c):
          win3->show();

          1 Reply Last reply Reply Quote 2
          • Albator
            Albator last edited by

            This post is deleted!
            1 Reply Last reply Reply Quote 0
            • Albator
              Albator last edited by

              I try getter/setter :

              in my first windows second_p2

              // seconde_p2.h
              
                  //getter
                  int get_c() const{return c;}
                  //setter
                  void set_c(int val);
              
              protected :
                  int c;
              
              // seconde_p2.cpp
              seconde_p2::seconde_p2(QWidget *parent) :
                  QMainWindow(parent),
                  ui(new Ui::seconde_p2)
              {
                  ui->setupUi(this);
              
                  c=3;
              
              
              }
              

              yes for this example setter is useless and in my seconde page alias seconde_p3 i have :

              // seconde_p3.cpp
              
              seconde_p3::seconde_p3(QWidget *parent) :
                  QMainWindow(parent),
                  ui(new Ui::seconde_p3)
              {
                  ui->setupUi(this);
              
              
              
              seconde_p2* test = new seconde_p2();
              test->get_c();
              qDebug()<< test;
              
              
              
              }
              

              i have 0 error but ! in the console i can look this :
              seconde_p2(0x1bf26bf8, name="seconde_p2")

              and it's not my c=3 :(

              B 1 Reply Last reply Reply Quote 0
              • B
                Bonnie @Albator last edited by Bonnie

                @Albator said in communicate data between two windows:

                i have 0 error but ! in the console i can look this :
                seconde_p2(0x1bf26bf8, name="seconde_p2")

                and it's not my c=3 :(

                [EDITED]
                Sorry, didn't understand your code until now...

                qDebug()<< test->get_c();
                

                That should output a "3"

                1 Reply Last reply Reply Quote 2
                • Albator
                  Albator last edited by

                  yes it's exact ! thanks all for your help ! :)))

                  1 Reply Last reply Reply Quote 0
                  • Albator
                    Albator last edited by Albator

                    i have a little problem, for a int it's okay but to make a getter with a qvector<double> it doesn't work.

                    seconde_p2.h
                     //getter
                        int get_c() const{return c;}
                        QVector<double> get_vector() const{return fer;}
                        //setter
                        //void set_c(int val);
                    
                    protected :
                        int c=6;
                        QVector<double> fer;
                    
                    // seconde_p2.cpp
                    seconde_p2::seconde_p2(QWidget *parent) :
                        QMainWindow(parent),
                        ui(new Ui::seconde_p2)
                    {
                        ui->setupUi(this);
                    
                        c=3;
                        fer={ 1, 2, 3, 4, 5, 6, 7 };
                    
                    
                    }
                    

                    and in the seconde_p3.cpp

                    seconde_p3::seconde_p3(QWidget *parent) :
                        QMainWindow(parent),
                        ui(new Ui::seconde_p3)
                    {
                        ui->setupUi(this);
                    
                    seconde_p2* test = new seconde_p2();
                    seconde_p2* test1 = new seconde_p2();
                    
                    test->get_vector();
                    qDebug()<<"qvector<double> display"<< test->get_vector();
                    
                    test1->get_c();
                    qDebug()<<"int display"<< test1->get_c();
                    
                    
                    }
                    

                    in the console :

                    qvector<double> display QVector() // why he is empty ?
                    int display 3

                    1 Reply Last reply Reply Quote 0
                    • Albator
                      Albator last edited by

                      This post is deleted!
                      1 Reply Last reply Reply Quote 0
                      • mrjj
                        mrjj Lifetime Qt Champion last edited by

                        Hi
                        Should work as you put value in the vector, in the constructor.

                        what does test->get_vector();
                        do ?

                        maybe you return empty list there ?

                        1 Reply Last reply Reply Quote 0
                        • Albator
                          Albator last edited by

                          @Albator said in communicate data between two windows:

                          QVector<double> get_vector() const{return fer;}

                          and fer is not empty, it works for c ...

                          1 Reply Last reply Reply Quote 0
                          • Q
                            Q139 last edited by

                            Is it wrong to use global window pointers and access setters getters via pointer instead of signals&slots?

                            1 Reply Last reply Reply Quote 0
                            • Albator
                              Albator last edited by

                              i want use getter in first but if i have not the choice maybe i will change my solution, it works for c so it should work for the qvector

                              mrjj 1 Reply Last reply Reply Quote 0
                              • mrjj
                                mrjj Lifetime Qt Champion @Albator last edited by

                                @Albator
                                yes must also work for Qvector same way as int.

                                Albator 1 Reply Last reply Reply Quote 0
                                • Albator
                                  Albator @mrjj last edited by

                                  @mrjj
                                  that's what I think, but who knows where the pangolin is that's standing in the way of my code?

                                  mrjj 1 Reply Last reply Reply Quote 0
                                  • mrjj
                                    mrjj Lifetime Qt Champion @Albator last edited by

                                    @Albator
                                    so while it returns empty list and the same time in same code and
                                    in same run, it can return 3 for c ?
                                    it seems impossible :)

                                    Albator 1 Reply Last reply Reply Quote 1
                                    • Albator
                                      Albator @mrjj last edited by

                                      @mrjj
                                      in first i put my value :

                                      seconde_p2::seconde_p2(QWidget *parent) :
                                          QMainWindow(parent),
                                          ui(new Ui::seconde_p2)
                                      {
                                          ui->setupUi(this);
                                      
                                          c=3;
                                          fer={ 1, 2, 3, 4, 5, 6, 7 };
                                          qDebug() << fer;
                                      
                                      
                                      }
                                      

                                      and in second i have a button who call the other windows :

                                      void seconde_p2::on_page3_clicked()
                                      {
                                          p3 = new seconde_p3(this);
                                          p3->show();
                                      }
                                      

                                      and for the final in the second_p3.cpp (my other window), i use my getter and display :

                                      seconde_p3::seconde_p3(QWidget *parent) :
                                          QMainWindow(parent),
                                          ui(new Ui::seconde_p3)
                                      {
                                          ui->setupUi(this);
                                      
                                      seconde_p2* test = new seconde_p2();
                                      seconde_p2* test1 = new seconde_p2();
                                      
                                      test->get_data1();
                                      qDebug()<<"qvector<double> display"<< test->get_data1();
                                      
                                      test1->get_c();
                                      qDebug()<<"int display"<< test1->get_c();
                                      
                                      
                                      }
                                      

                                      and tadaaaa : c is displayed correctly and fer is empty
                                      my conclusion ? it's a sad day :(

                                      1 Reply Last reply Reply Quote 0
                                      • Christian Ehrlicher
                                        Christian Ehrlicher Lifetime Qt Champion last edited by

                                        @Albator said in communicate data between two windows:

                                        seconde_p2* test = new seconde_p2();
                                        seconde_p2* test1 = new seconde_p2();

                                        C++ basics - this are new local instances - it won't help at all. You have to pass the pointer from the other object to this class. This is basic c++ stuff - programming with Qt without this basic knowledge will not work.

                                        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                                        Visit the Qt Academy at https://academy.qt.io/catalog

                                        Albator 1 Reply Last reply Reply Quote 3
                                        • Albator
                                          Albator @Christian Ehrlicher last edited by

                                          @Christian-Ehrlicher
                                          excuse me I try and thanks to your advices I advance and yes sometimes I miss my bases :)

                                          1 Reply Last reply Reply Quote 0
                                          • First post
                                            Last post