PDA

View Full Version : Communication between MainWindow and a dialog



Backslash
2nd August 2007, 21:33
Hello again,

I have a main window that opens a dialog when a button is pressed.
When this happens it sends an instance of a class to the dialog for processing like so...


void on_push_0_clicked()
{
KeyDialog dialog(this); //Create dialog
dialog.setKey(zero); //use setKey function of dialog
dialog.exec(); //Show dialog
}

(Michiel helped me with that one:))

Now I want to send the processed instance back to the main window.


void KeyDialog::on_buttonBox_accepted()
{
//Copy key for safety
Key::Key K2 = K1; //K1 is a global instance that is processed earlier in the form
//add new data from form
//changes vars in K2
mainWin.resetKey(K2); //sends K2 back to mainwindow
}

But I guess my dialog has no knowledge of MainWindow because it says it hasn't been declared. How do I go about accesing this member function of MainWindow?

Thank you all very much for your help,
Backslash

marcel
2nd August 2007, 21:41
Since you create the dialog with the main window as parent, you could do a cast to get an instance of the window type:


((MainWindowClass*)parent())->resetKey(K2);


Regards

Backslash
2nd August 2007, 22:06
I'm sorry, but I don't quite understand what doing a cast means.

The MainWindow instance is called mainWin (from main.cpp)


QApplication app(argc, argv);
MainWindow mainWin;
mainWin.show();
return app.exec();

So do you mean for me to write:

((MainWindow*)parent())->resetKey(K2);

or something else entirely?

Thanks,
Backslash

marcel
2nd August 2007, 22:09
Yes, the easiest way is to do that.
In on_buttonBox_accepted, replace the last line with:


((MainWindow*)parent())->resetKey(K2);


Should work.

To find out what a cast is, do a search on google on"c++ inheritance and polymorphism".

Regards

Backslash
2nd August 2007, 22:21
Thanks again for the help,

When I use that line "((MainWindow*)parent())->resetKey(K2);" I get this error message from make:

keydialog.cpp: In member function `void KeyDialog::on_buttonBox_accepted()':
keydialog.cpp:117: error: `MainWindow' undeclared (first use this function)
keydialog.cpp:117: error: (Each undeclared identifier is reported only once for
each function it appears in.)
keydialog.cpp:117: error: expected primary-expression before ')' token
keydialog.cpp:117: error: expected `)' before "parent"
mingw32-make[1]: *** [release\keydialog.o] Error 1

Did I misunderstand something?

marcel
2nd August 2007, 22:25
You have to include MainWindow.h in keydialog.cpp.

A more elegant solution is to create a getter in the keydialog. This will return the key (K2).
So, in the slot on_push_0_clicked, you can do:


if(dialog.exec() == QDialog::Accepted )
{
Key::Key k = dialog.getKey2();
resetKey(k);
}
Regards

Backslash
2nd August 2007, 22:29
Wow, that is a neat solution. Definitely going to try that one...

Thanks for the patience and the insight,
Backslash

Backslash
2nd August 2007, 23:31
Is this the order in which I should put this?


void MainWindow::on_push_0_clicked()
{
KeyDialog dialog(this);
dialog.setKey(zero);
dialog.exec();
if(dialog.exec() == QDialog::Accepted )
{
Key::Key k = dialog.getKey();
resetKey(k);
}
}

In KeyDialog, My getKey(); looks like this:


Key getKey()
{
return K2;
}

(K2 is defined at the top of KeyDialog because I use on_buttonBox_accepted to take the current state of the form's widgets and translate those into values to be stored in K2)

All of this seems ok to me but I'm getting a wierd error from make:

release\mainwindow.o(.text+0x8455):mainwindow.cpp: undefined reference to `KeyDi
alog::getKey()'
release\mainwindow.o(.text+0x9915):mainwindow.cpp: undefined reference to `KeyDi
alog::getKey()'
release\mainwindow.o(.text+0xadd5):mainwindow.cpp: undefined reference to `KeyDi
alog::getKey()'
release\mainwindow.o(.text+0xc295):mainwindow.cpp: undefined reference to `KeyDi
alog::getKey()'
release\mainwindow.o(.text+0xd755):mainwindow.cpp: undefined reference to `KeyDi
alog::getKey()'
release\mainwindow.o(.text+0xec15):mainwindow.cpp: more undefined references to
`KeyDialog::getKey()' follow
collect2: ld returned 1 exit status

I have no clue what that's about...

Thanks again,
Backslash

marcel
2nd August 2007, 23:40
Well, this looks like c++ mistake:
When you implement a function in a class, you have to prepend the name of the class and the resolution operator to it:


Key::Key KeyDialog::getKey()
{
return K2;
}
Now, K2 has to be a member of the class in order to be visible in any of the class's member functions.

Also, the correct thing to do is:


void MainWindow::on_push_0_clicked()
{
KeyDialog dialog(this);
dialog.setKey(zero);
if(dialog.exec() == QDialog::Accepted )
{
Key::Key k = dialog.getKey();
resetKey(k);
}
}
exec(), by default, returns an exit code which shows you whether the user pressed ok or cancel( accepted or rejected).
So you need to call exec only once.

The compile errors tell you that you did not declared/defined the getKey function.
To declare a function - means you add it in the class declaration, in the header:


class KeyDialog : ...
{
public:
Key::Key getKey();
};
The function definition means the implementation. It appears that you have donh that( incorrectly although ).

So, fix these errors and try again.

Regards

Backslash
3rd August 2007, 05:27
Thanks for all the help. I'm glad there's a newbie section of this forum.
Now I just have to work out some more kinks in my code and implement a few more functions.

Thanks again,
Backslash