PDA

View Full Version : How to access controls of one window from another?



gtan
31st August 2012, 17:09
I have created a Qt Gui application which has two forms: mainwindow.ui and childwindow.ui.

I have a LineEdit controls on both forms. How can I access the text on the mainwindow from the childwindow and vice-versa?

Normally, on the same from I will use the code:


ui->lineEdit1->text();

Coises
3rd September 2012, 02:21
I have created a Qt Gui application which has two forms: mainwindow.ui and childwindow.ui.

I have a LineEdit controls on both forms. How can I access the text on the mainwindow from the childwindow and vice-versa?

Normally, on the same from I will use the code:


ui->lineEdit1->text();


There are multiple ways you could go about this, depending on the structure of your application and how much it is appropriate that one window should “know” about the other.

For example, you could add a parameter to the constructor for the child window and pass it a QLineEdit* that is the address of lineEdit1 in the main window. The child window would then save that pointer and be able to access the main window’s line edit through it.

The child window could declare a QLineEdit* in the public part of its interface and copy the pointer to its line edit there in the constructor. So long as the main window keeps a copy of the pointer to the child window, it would then also have access to the child window’s line edit.

There is no formula for this; you have to design methods by which the two objects (windows) will communicate.

gtan
21st September 2012, 13:45
Alright. Thank you!