Results 1 to 7 of 7

Thread: Killing a Window in its constructor

  1. #1
    Join Date
    May 2006
    Posts
    58
    Thanks
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Killing a Window in its constructor

    How do I kill a Window in its constructor (or some other startup event) based upon some failure?

    I have tried directly calling close() in the constructor, but this isn't working. Is there some sort of signal that is emitted after the constructor, but on the initial creation of the window, from which I can successfully call the close method?

  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: Killing a Window in its constructor

    You can use some sort of splash screen / window to do the startup events. If events are successful, display the main window, else dont create the main window at all.

  3. #3
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Killing a Window in its constructor

    Calling close() in a constructor doesn't have much effect because most likely you are calling show() later anyway. And the event loop isn't even running by that time. One solution would be to use QTimer::singleShot() with a 0ms delay to close the window or maybe even QObject::deleteLater() to schedule a delete when the control reaches the event loop. However, I think you'll just end up having a flickering window. The window will be closed as soon as it appears but most likely it is still possible notice.

    Maybe a sufficient solution would be to move part of the code in the constructor to a separate "bool Window::initialize(..)" function or similar. Then you can simply avoid calling show() at all if the initialization fails.
    J-P Nurmi

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Killing a Window in its constructor

    Or you can set a flag in the constructor (instead of having an additional method for initialisation) whether the widget initialised properly and then when showing the window, check that condition first:
    Qt Code:
    1. MyWidget w;
    2. if(w.isValid()) w.show();
    To copy to clipboard, switch view to plain text mode 

  5. The following user says thank you to wysota for this useful post:

    hardgeus (14th December 2006)

  6. #5
    Join Date
    May 2006
    Posts
    58
    Thanks
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Killing a Window in its constructor

    Quote Originally Posted by wysota View Post
    Or you can set a flag in the constructor (instead of having an additional method for initialisation) whether the widget initialised properly and then when showing the window, check that condition first:
    Qt Code:
    1. MyWidget w;
    2. if(w.isValid()) w.show();
    To copy to clipboard, switch view to plain text mode 
    Thanks! You always have the clean solution that makes me smack my forehead

  7. #6
    Join Date
    Aug 2006
    Posts
    44
    Thanked 7 Times in 7 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Killing a Window in its constructor

    You could also:
    1. call deleteLater()
    2. post a custom event to your application and tell it to delete the object
    3. put window creation in a try/catch and throw an exception

  8. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Killing a Window in its constructor

    Quote Originally Posted by Eldritch View Post
    You could also:
    1. call deleteLater()
    This is risky, the object may be allocated on stack. You'd have to forbid stack allocation.

Similar Threads

  1. right way to open a new window
    By wind in forum Newbie
    Replies: 1
    Last Post: 1st November 2006, 11:17
  2. Replies: 5
    Last Post: 4th August 2006, 23:44
  3. Replies: 3
    Last Post: 23rd July 2006, 18:02
  4. Move window in Clone Mode
    By ultrabrite in forum Qt Programming
    Replies: 1
    Last Post: 14th June 2006, 18:22
  5. cannot make a main window modal
    By Dark_Tower in forum Qt Programming
    Replies: 12
    Last Post: 23rd March 2006, 10:21

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.