communicate data between two windows
-
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; }
-
Hi,
You can use Signals and Slots, or a simple setter, or pass the value as a parameter of the constructor.
-
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(); -
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 :(
-
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 :(
@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"
-
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 -
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 ?
-
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
that's what I think, but who knows where the pangolin is that's standing in the way of my code? -
@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 :)@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 :( -
@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.
-
@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.
@Christian-Ehrlicher
excuse me I try and thanks to your advices I advance and yes sometimes I miss my bases :)