Results 1 to 18 of 18

Thread: Creating a Modeless Dialog that responds to a close event

  1. #1
    Join Date
    May 2009
    Posts
    18
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Creating a Modeless Dialog that responds to a close event

    Hello,

    I'm currently trying to create a modeless dialog box (or QMessageBox) using Qt 4.3 that:

    1. gets created in my Main class
    2. when it closes, it will call another function in the Main class that will exit the program.

    In other words, I am looking for a modeless dialog box that will perform an exit once the users closes the dialog box. I've looked at some posts on the internet and a few in this forum but I can't seem to figure out how to do it. This thread was a little helpful:

    http://www.qtforum.org/post/53763/re...less#post53763

    where it gave the idea of connecting the destroyed() event to a slot, or reimplement closeEvent(). Here is a little portion of my code:

    Qt Code:
    1. QMessageBox* pMessageBox = new QMessageBox(QMessageBox::Information,
    2. QObject::tr("My Software"),
    3. QObject::tr("Text in my dialog box."),
    4. 0,
    5. Qt::Dialog | Qt::WindowStaysOnTopHint );
    6.  
    7. pMessageBox->setModal(false);
    8. pMessageBox->show();
    9. //pMessageBox->activateWindow();
    10.  
    11. //connect(pMessageBox, SIGNAL(closeEvent()), this, SLOT(exitApp()));
    12. connect(pMessageBox, SIGNAL(destroyed()), this, SLOT(exitApp()));
    To copy to clipboard, switch view to plain text mode 

    But I don't seem to get the same result. The breakpoint in exitApp() never gets triggered.

    I got a little excited when I saw documentation that you can set a flag in QMessageBox to make it modeless, but this was in another version of Qt and not in Qt 4.3.

    Any ideas will be greatly appreciated.

    Kat

  2. #2
    Join Date
    May 2009
    Posts
    15
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Creating a Modeless Dialog that responds to a close event

    Connect the slot to something where you know you'll get output from (at least see if it's triggering). Does exitApp() when called on its own (without your connect) exit the application properly?

  3. #3
    Join Date
    May 2009
    Posts
    18
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Creating a Modeless Dialog that responds to a close event

    Hi Mike,

    Thanks for your reply. Yes, exitApp() works properly and will exit the program. Here is function definition:

    Qt Code:
    1. void CMainWindow::exitApp()
    2. {
    3. QApplication::exit(0);
    4. }
    To copy to clipboard, switch view to plain text mode 

    I believe it has something to do with the events I'm limited to. I've read in a few places that modeless dialogs will only destroy itself (or emit the destoryed() signal) if a flag is set (Qt::WA_DeleteOnClose ... I think?) But I can't seem to find the right place in my constructor to add this flag. Maybe I'm just not doing it correctly...

    In addition, the closeEvent() event doesn't work either...

    Kat

  4. #4
    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: Creating a Modeless Dialog that responds to a close event

    You could have also used -
    Qt Code:
    1. connect(pMessageBox, SIGNAL(destroyed()), qApp, SLOT(quit()));
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    May 2009
    Posts
    18
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Creating a Modeless Dialog that responds to a close event

    Thanks for your reply. Yes, this is true, however, it still doesn't work because I don't think the destroyed() signal is sent out for my application to catch it...

  6. #6
    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: Creating a Modeless Dialog that responds to a close event

    You forget that when you close a window, it doesn't get deleted. Use the WA_DeleteOnClose attribute to destoy a closed widget.
    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.


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

    kyue (29th May 2009)

  8. #7
    Join Date
    May 2009
    Posts
    18
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Creating a Modeless Dialog that responds to a close event

    Thank you. I was a little lost at how I was suppose to set that attribute. After re-reading the documentation there is a function QWidget::setAttribute() available for use. It makes sense now...

    Here it is just in case for anyone else out there who might need it

    Qt Code:
    1. pMessageBox->setModal(false);
    2. pMessageBox->setAttribute(Qt::WA_DeleteOnClose);
    3. pMessageBox->show();
    4. //pMessageBox->activateWindow();
    5.  
    6. //connect(pMessageBox, SIGNAL(closeEvent()), this, SLOT(exitApp()));
    7. connect(pMessageBox, SIGNAL(destroyed()), this, SLOT(exitApp()));
    To copy to clipboard, switch view to plain text mode 

  9. #8
    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: Creating a Modeless Dialog that responds to a close event

    One thing interests me in your code - what is your usecase of using a non-modal message box that terminates the application when closed? Message boxes are usually modal...
    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.


  10. #9
    Join Date
    May 2009
    Posts
    18
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Creating a Modeless Dialog that responds to a close event

    What happens is that the user is notified that the application will now close, through a dialog box. The code is written where the Main thread and UI thread are the same. Therefore, if I had a modal dialog box open and I needed the main thread to process an incoming message, the main thread waits and tries to obtain a mutex on the same thread and my program crashes. Therefore, I needed a modeless dialog box where it doesn't lock my main thread.

  11. #10
    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: Creating a Modeless Dialog that responds to a close event

    What do you mean by "main thread" and "ui thread"?
    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.


  12. #11
    Join Date
    May 2009
    Posts
    18
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Creating a Modeless Dialog that responds to a close event

    The "main thread" is the main application thread. "UI thread" is the thread that handles User Interface messages and processes them.

    So I have 1 thread that will handle and process my main application operations as well as UI operations. In my situation, this 1 thread was sitting and waiting for my modal dialog box to close, while there was another request from the "main thread" that needed to be processed.

    I hope that helped

  13. #12
    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: Creating a Modeless Dialog that responds to a close event

    So you have one or two threads in your application?
    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.


  14. #13
    Join Date
    May 2009
    Posts
    18
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Creating a Modeless Dialog that responds to a close event

    We have a few, but the UI thread is unfortunately sitting on the same thread as this main thread.

  15. #14
    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: Creating a Modeless Dialog that responds to a close event

    And where do those messages come from? Other threads or the same thread? What kind of events are they?
    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.


  16. #15
    Join Date
    May 2009
    Posts
    18
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Creating a Modeless Dialog that responds to a close event

    The messages come from the network (LAN). We have wrapper classes that we use that we call the DataChannel classes. These messages come in, the application receives it, processes it, and sends the request to the respectful handler.

    So throughout our code, we use modeless dialog boxes so that the thread will be free to receive and process these DataChannel messages.

  17. #16
    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: Creating a Modeless Dialog that responds to a close event

    Socket notifiers might indeed be blocked when a dialog is executed but there is a way to override it if you want. You have to provide a method with exact same contents as exec() but with different parameters sent to QEventLoop::exec(). If you add clear the QEventLoop::ExcludeSocketNotifiers flag, you should get a modal dialog with network processing. The simplest implementation is to just create the event loop, set modality and call exec() on the event loop.
    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.


  18. #17
    Join Date
    May 2009
    Posts
    18
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Creating a Modeless Dialog that responds to a close event

    Thank you for your advice. I am currently working on a port from Windows->Mac and I am required to use the existing libraries and how it is currently written. We receive these messages in our wrapper classes and only 2% of them are Qt projects. So I was asked to find a modeless dialog box on Qt as a solution.

  19. #18
    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: Creating a Modeless Dialog that responds to a close event

    I'm not telling you to modify Qt, just to provide an additional method that will re-enable socket notifiers.
    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.


Similar Threads

  1. Replies: 3
    Last Post: 1st February 2009, 15:49
  2. Replies: 9
    Last Post: 13th August 2008, 18:07
  3. Close Dialog in showEvent
    By pospiech in forum Qt Programming
    Replies: 3
    Last Post: 11th April 2008, 15:32
  4. Replies: 3
    Last Post: 23rd July 2006, 18:02

Tags for this Thread

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.