Is there a way to pair 2 top level windows?
Hi all,
I'm new to Qt programming so please bare with me. I'm currently working with some code that has 2 top level windows open simultaneously (lets name them window 1 and window 2). The control flow of the software design demands that window 1 acts as a pseudo parent to window 2 and therefore window 2 should close every time window 1 is closed. However, the opposite will not be true. Since both windows are independent of each other, is there a way of achieving this or am I banging my head against a wall here?
Note: Unfortunately making window a child widget to window one is not possible due to the way the code has already been developed.
Thanks in advance.
Re: Is there a way to pair 2 top level windows?
You can make use of signals and slots.
Just inform one another about the events you are interested in and decide upon the action
Re: Is there a way to pair 2 top level windows?
Thanks for the response. What signal should I be using to determine if the X (close) button on window 1 has been clicked?
Re: Is there a way to pair 2 top level windows?
Install an event filter to detect QEvent::Close, e.g.
Code:
#include <QtGui>
class EventSignaller
: public QObject{
Q_OBJECT
signals:
void closing();
protected:
{
if(event
->type
() == QEvent::Close) emit(closing());
return QObject::eventFilter(object, event
);
}
};
int main(int argc, char *argv[])
{
w1.setWindowTitle("w1");
w1.show();
w2.setWindowTitle("w2");
w2.show();
EventSignaller s;
w1.installEventFilter(&s);
w2.connect(&s, SIGNAL(closing()), SLOT(close()));
return app.exec();
}
#include "main.moc"