PDA

View Full Version : how can i access other form widget



crissti_cta
14th December 2010, 18:38
Hello.

i have a main form and form2.ui

in the main form i have the following code:



void MainWindow::on_pushButton_2_clicked()
{
Form2 *f2 = new Form2;
f2->show();
}

so i started the form2.

in the form2 i have a textEdit widget.
1. How can i change the text from main form?
2. How can i click a f2's pushButton or how can i call a function declared in form2.
Thanks.

Zlatomir
14th December 2010, 19:16
You should read about signals and slots (http://doc.trolltech.com/4.7/signalsandslots.html)

And i really recommend you to stop naming the slots for the auto-connect (the on_WidgetName_SignalName() naming convention) and first learn about signals, slots and how the connection works, then you can use auto-connect if you find it useful.

squidge
14th December 2010, 20:51
Secondly, think about encapsulation. Changing the text of a text edit widget from a different form than the owner is not recommended and isn't possible by default (the members are all private). Instead, create a method that takes a class pointer (or in this case, even just a QString, but think about the future and ease of expandability...) and then let the form use that class/QString to populate itself.