Results 1 to 5 of 5

Thread: How to exit in a QDialog constructor

  1. #1
    Join Date
    Dec 2009
    Posts
    2
    Qt products
    Qt4
    Platforms
    Windows

    Question How to exit in a QDialog constructor

    Hi All,

    I want to know how to close a Dialog in its constructor. (without using QTimer:singlshoot).


    Thanks in advance to all.

  2. #2
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to exit in a QDialog constructor

    you can use QMetaObject::invokeMethod with Qt::QueuedConnection, i.e.
    Qt Code:
    1. MyDgl::MyDlg(QWidget *parent)
    2. : QDialog(parent)
    3. {
    4. ...
    5. QMetaObject::invokeMethod(this, "close", Qt::QueuedConnection);
    6. }
    To copy to clipboard, switch view to plain text mode 
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  3. #3
    Join Date
    Dec 2009
    Posts
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to exit in a QDialog constructor

    Thanks.

    I test it, it works, but we see the dialog in short laps of time before it is closed.
    Do you know how to bypass this "cosmetic" issue ?

  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: How to exit in a QDialog constructor

    Until you show the dialog, it is hidden. So simply don't show it at all if you don't want to.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Nov 2009
    Posts
    129
    Thanks
    4
    Thanked 29 Times in 29 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to exit in a QDialog constructor

    Quote Originally Posted by ms77 View Post
    I want to know how to close a Dialog in its constructor. (without using QTimer:singlshoot).
    I think there’s a slight misunderstanding implied by your question.

    A constructor constructs an object of the specified type — short of throwing an exception (not the way to go here!) it will always construct an object — but a constructed QDialog object doesn’t do anything until you call exec(), open() or show() on it.

    One of those functions is normally called by the same code that calls the constructor. If that’s the case in your program, then what you want to do is not “close the dialog in its constructor” but supply a return value (if the caller uses it) and keep the caller from ever calling QDialog::exec(), QDialog::open () or QWidget::show().

    If the calling code just constructs the dialog and then calls exec(), write another member function and call it instead of exec():

    Qt Code:
    1. int execConditional() {return doNotOpenThisDialog ? valueWhenNotOpened : exec();}
    To copy to clipboard, switch view to plain text mode 

    If you do something more involved, such as using a modeless dialog, you might need to emit a signal or two when bypassing display of the dialog.

    The one case I can imagine where this could get quite complicated is if you must pass your constructed object to other code which knows only that it is a QDialog, but has no information about your sub-class. Since the exec(), open() and show() slots are not virtual, you can’t just override them if they’re going to be called on the underlying QDialog object.

    But if you’re not doing something like that — if all the code that interacts with your dialog knows what dialog it is, not just that it’s some sort of dialog — then use the straightforward method and just have the code call separate member functions that wrap exec(), open() and/or show() to avoid displaying the dialog when it’s not desired.

Similar Threads

  1. Replies: 5
    Last Post: 1st March 2010, 15:55
  2. Abort QDialog in the constructor
    By Tino in forum Qt Programming
    Replies: 2
    Last Post: 29th July 2009, 15:36
  3. KDE/QWT doubt on debian sarge
    By hildebrand in forum KDE Forum
    Replies: 13
    Last Post: 25th April 2007, 06:13
  4. QDialog: show() and exec() together in constructor?
    By Teuniz in forum Qt Programming
    Replies: 8
    Last Post: 28th February 2007, 11:43

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.