whatWhat
whatWhat

Reputation: 4077

Passing variables from one form to another in Qt

I've got two forms, the mainform which opens up a dialog box that has a text box in it. How can I pass the text from the textbox back to the mainform? I've tried alot of different methods but I think I'm missing something simple. Thanks for any help.

Upvotes: 4

Views: 2925

Answers (2)

Mark Beckwith
Mark Beckwith

Reputation: 1992

The dialog box still exists after it closes. So you can, from the main form, do something like this:

QString text = subform->textEdit->text();

This assumes your dialog box is subform and the name you gave to the text edit box is textEdit. Make sure you make textEdit public in the designer.

If you don't want to make textEdit public, then you can add a getter to subform.

Upvotes: 3

AdamC
AdamC

Reputation: 16283

If you use the MVC pattern, you create the model object (container for your data) and pass to the text box to fill in the text value itself. When the dialog is closed, just read the value from the model and put it wherever you need it.

Upvotes: 0

Related Questions