Reputation: 1210
i have 2 modeless dialogs, both are ran from the mainwindow. Basically, i'm trying to pass a string by pushing a button from the first one to the second one with no success. so both of the dialogs are declared in the mainwindow.h like that:
Dialog1 *dg1;
Dialog2 *dg2;
and created in mainwindow.cpp:
dg1 = new Dialog1(this);
dg2 = new Dialog2(this);
i need to pass the string from dg1 to dg2 by calling the function that updates the dg2's textEdit. I tried:
#include "Dialog2.h"
...
Dialog2 dg2;
dg2.callfunction(string);
but no sign that the string got there. How do i do that?
Upvotes: 0
Views: 42
Reputation: 9166
There are three ways to solve this:
Signal and slot: Create a signal for dialog1 and a slot for dialog2. Both must have a QString as parameter. Connect them in the main window where the dialogs are constructed.
Relation by setter: Give dialog1 a setter that accepts a pointer to dialog2. Memorize the dialog2 pointer in a member variable. Call the callfunction of dialog2 in dialog1 using that pointer. Call the pointer-setter where the dialogs are constructed. Don't forget to clear the pointer if dialog2 isn't available anymore and to check pointer validity before accessing it.
Relation by reference: Add a dialog2 reference as parameter to the constructor of dialog1. Memorize that referenze in a member variable. Call the callfunction of dialog2 in dialog1 using that reference. You need to construct dialog2 before dialog1 to do this. This works only safely if dialog2 always lives longer than dialog1.
You should be able to move forward with that information.
I would go with 1.
I suggest to read more about the needed fundamentals. Read about Qt's signals and slots. Read about C++ pointers, references and constructors.
Upvotes: 1