Results 1 to 4 of 4

Thread: Is there a way to pair 2 top level windows?

  1. #1
    Join Date
    Jul 2010
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default 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.

  2. #2
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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

  3. #3
    Join Date
    Jul 2010
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default 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?

  4. #4
    Join Date
    Jul 2010
    Posts
    21
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Is there a way to pair 2 top level windows?

    Install an event filter to detect QEvent::Close, e.g.

    Qt Code:
    1. #include <QtGui>
    2.  
    3. class EventSignaller : public QObject
    4. {
    5. Q_OBJECT
    6.  
    7. signals:
    8. void closing();
    9.  
    10. protected:
    11. bool eventFilter(QObject *object, QEvent *event)
    12. {
    13. if(event->type() == QEvent::Close)
    14. emit(closing());
    15.  
    16. return QObject::eventFilter(object, event);
    17. }
    18. };
    19.  
    20. int main(int argc, char *argv[])
    21. {
    22. QApplication app(argc, argv);
    23.  
    24. QWidget w1;
    25. w1.setWindowTitle("w1");
    26. w1.show();
    27.  
    28. QWidget w2;
    29. w2.setWindowTitle("w2");
    30. w2.show();
    31.  
    32. EventSignaller s;
    33. w1.installEventFilter(&s);
    34. w2.connect(&s, SIGNAL(closing()), SLOT(close()));
    35.  
    36. return app.exec();
    37. }
    38.  
    39. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 0
    Last Post: 22nd March 2010, 18:00
  2. A pair of emails questions
    By Raccoon29 in forum Qt Programming
    Replies: 4
    Last Post: 28th December 2009, 23:10
  3. Replies: 3
    Last Post: 27th December 2009, 00:00
  4. How create a two level table?
    By kaushal_gaurav in forum Qt Programming
    Replies: 3
    Last Post: 29th October 2008, 18:16
  5. Replies: 4
    Last Post: 4th June 2007, 12:07

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.