PDA

View Full Version : Is there a way to pair 2 top level windows?



Naveed
21st July 2010, 21:36
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.

aamer4yu
22nd July 2010, 05:59
You can make use of signals and slots.
Just inform one another about the events you are interested in and decide upon the action

Naveed
23rd July 2010, 22:10
Thanks for the response. What signal should I be using to determine if the X (close) button on window 1 has been clicked?

hobbyist
23rd July 2010, 23:57
Install an event filter to detect QEvent::Close, e.g.



#include <QtGui>

class EventSignaller : public QObject
{
Q_OBJECT

signals:
void closing();

protected:
bool eventFilter(QObject *object, QEvent *event)
{
if(event->type() == QEvent::Close)
emit(closing());

return QObject::eventFilter(object, event);
}
};

int main(int argc, char *argv[])
{
QApplication app(argc, argv);

QWidget w1;
w1.setWindowTitle("w1");
w1.show();

QWidget w2;
w2.setWindowTitle("w2");
w2.show();

EventSignaller s;
w1.installEventFilter(&s);
w2.connect(&s, SIGNAL(closing()), SLOT(close()));

return app.exec();
}

#include "main.moc"