[SOLVED] Close signal send to other class
-
Hello. I have two classes, QMainWindow and QDialog. Each class has separate GUI.
In main function I first call QMainWindow (with gui), then after some operations I want to close this gui (this->close()) , and send signal to 2nd class to open. Is this possible?I was trying sth like that:
main.cpp
@ class1 w;
w.show();
if(!w.isVisible())
{
class2 u;
u.show();
}@but it's not working :(
-
There are mainly two problems here:
- the way the code is written, u is an automatic variable local to the if() block, so in the example it immediately goes out of scope after the u.show() call and is therefore freed.
Use a dynamic instance instead which will remain existant:
class2* u = new class2;
u->show(); - w.show() is does not result in a modal window, instead it makes the window visible and then returns immediately. The visibility check in the if statement, which is executed next, therefore always evaluates to true. In order to create u as desired (ie when w gets closed), declare a slot in class1 and connect it to the close() signal. Execute the creation code in the slot implementation.
- the way the code is written, u is an automatic variable local to the if() block, so in the example it immediately goes out of scope after the u.show() call and is therefore freed.
-
You can try with this code. I'm assuming that you are writing your own classes which are inherited from QMainWindow and QDialog. There is no close signal when the mainwindow is closed. You need to implement your own signal for mainwindow.
@class MainWindow : public QMainWindow{
signals :
void iamClosed();
public slots :
void closeEvent() {
emit iamClosed();
}
}main() {
MainWindow m;
Dialog d;
connect(&m,SIGNAL(iamClosed()),&d,SLOT(show()));
}@ -
@chrisberlin, I forgot that it's non-modal window, thanks ;) I tried sth like your way, and it works too, i.e make slot in class1, which creates 2nd GUI and close itself, it works too.
@Dheerendra, thanks, I didn't know that signals can be declared in classes :) I must read more documentations...
I will use your way, I think that's more 'elegant'.