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. communicate data between two windows
QtWS25 Last Chance

communicate data between two windows

Scheduled Pinned Locked Moved Solved General and Desktop
19 Posts 6 Posters 1.9k 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.
  • AlbatorA Offline
    AlbatorA Offline
    Albator
    wrote on last edited by
    #1

    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
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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
      2
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #3

        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
        2
        • AlbatorA Offline
          AlbatorA Offline
          Albator
          wrote on last edited by
          #4
          This post is deleted!
          1 Reply Last reply
          0
          • AlbatorA Offline
            AlbatorA Offline
            Albator
            wrote on last edited by
            #5

            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
            0
            • AlbatorA Albator

              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 Offline
              B Offline
              Bonnie
              wrote on last edited by Bonnie
              #6

              @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
              2
              • AlbatorA Offline
                AlbatorA Offline
                Albator
                wrote on last edited by
                #7

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

                1 Reply Last reply
                0
                • AlbatorA Offline
                  AlbatorA Offline
                  Albator
                  wrote on last edited by Albator
                  #8

                  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
                  0
                  • AlbatorA Offline
                    AlbatorA Offline
                    Albator
                    wrote on last edited by
                    #9
                    This post is deleted!
                    1 Reply Last reply
                    0
                    • mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      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
                      0
                      • AlbatorA Offline
                        AlbatorA Offline
                        Albator
                        wrote on last edited by
                        #11

                        @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
                        0
                        • Q Offline
                          Q Offline
                          Q139
                          wrote on last edited by
                          #12

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

                          1 Reply Last reply
                          0
                          • AlbatorA Offline
                            AlbatorA Offline
                            Albator
                            wrote on last edited by
                            #13

                            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

                            mrjjM 1 Reply Last reply
                            0
                            • AlbatorA Albator

                              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

                              mrjjM Offline
                              mrjjM Offline
                              mrjj
                              Lifetime Qt Champion
                              wrote on last edited by
                              #14

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

                              AlbatorA 1 Reply Last reply
                              0
                              • mrjjM mrjj

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

                                AlbatorA Offline
                                AlbatorA Offline
                                Albator
                                wrote on last edited by
                                #15

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

                                mrjjM 1 Reply Last reply
                                0
                                • AlbatorA Albator

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

                                  mrjjM Offline
                                  mrjjM Offline
                                  mrjj
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #16

                                  @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 :)

                                  AlbatorA 1 Reply Last reply
                                  1
                                  • mrjjM mrjj

                                    @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 :)

                                    AlbatorA Offline
                                    AlbatorA Offline
                                    Albator
                                    wrote on last edited by
                                    #17

                                    @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
                                    0
                                    • Christian EhrlicherC Online
                                      Christian EhrlicherC Online
                                      Christian Ehrlicher
                                      Lifetime Qt Champion
                                      wrote on last edited by
                                      #18

                                      @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

                                      AlbatorA 1 Reply Last reply
                                      3
                                      • Christian EhrlicherC Christian Ehrlicher

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

                                        AlbatorA Offline
                                        AlbatorA Offline
                                        Albator
                                        wrote on last edited by
                                        #19

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

                                        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