The following code snippet opens two windows, w1 and w2. How can one force w2 to close when w1 is closed by the user? As in the comment, the connect
function is not working that way.
#include <QApplication>
#include <QtGui>
#include <QtCore>
#include <QtWidgets>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget w1;
w1.setWindowTitle("w1");
w1.show();
QWidget w2;
w2.setWindowTitle("w2");
w2.show();
// when w1 is closed by the user, I would like w2 to close, too.
// However, it won't happen, even though the code compiles fine.
QObject::connect(&w1, &QObject::destroyed, &w2, &QWidget::close);
return a.exec();
}
Edit In my case, those two widgets are designed in two separate libraries, so they cannot communicate with each other. Thus, the close event is not applicable.
Edit Eventually my solution to my problem is based on the answer by @JeremyFriesner: emit a closed
signal in closeEvent
of w1
, and connect this (instead of QObject::destroyed
) to w2
.