Results 1 to 6 of 6

Thread: QMessageBox question

  1. #1

    Default QMessageBox question

    Hi I'm encountering a problem with QMessageBox. I'm using Qt 4.3.3 here.
    I'm trying to dismiss a QMessageBox using a handle, not by user pressing the button. So say for example, I have a QMessageBox mb1 present, and power supply is plugged in, I want to dismiss mb1 (stop the exec() command) and show another QMessageBox mb2. I can easily do the dismiss by using a QMessageBox *handle = &mb1 and do handle->accept(). The problem is that I want to put a log message immediately either after mb1 is dismissed by the system or by the user. The exec() is blocked if 2 messageboxes are present. This sounds complicated so I'll provide my sudo code in below:

    Qt Code:
    1. void showMessageBox(QMessageBox **handle, QString& title)
    2. {
    3. QMessageBox mb = new QMessageBox(title, textBody, QMessageBox::Warning, QMessageBox::Ok);
    4. *handle = &mb;
    5. <Some message box format settings here>
    6. exec();
    7. printf("%s finished\n", title);
    8. if (handle != NULL)
    9. *handle = NULL;
    10. }
    11.  
    12. void main(void)
    13. {
    14. showMessageBox(&mb1, "mb1"); //call exec() 1st time
    15. if (power_supply_in)
    16. {
    17. mb1->accept(); //mb1 disappears on the screen
    18. printf("mb1 dismissed by system\n");
    19. showMessageBox(&mb2, "mb2"); //call exec() 2nd time
    20. }
    21. }
    To copy to clipboard, switch view to plain text mode 

    You will think that when you do the mb1->accept(), the first exec() is terminated. What I expected to see is:
    mb1 finished // when you do mb1->accept();
    mb1 dismissed by system // when you do printf("messagebox dismissed by system\n");
    mb2 finished //when the user press the "Ok" button on mb2

    However, what I saw was:
    mb1 dismissed by system //when you do printf("messagebox dismissed by system\n");
    mb2 finished //when the user press the "Ok" button on mb2, this is jumping out of the 2nd exec() command
    mb1 finished //the program is jumping out of the 1st exec() command

    Seems like the program is stuck in the exec() if there are multiple QMessageBox in action. Can anyone explain why and how to get my expected results? Maybe there's an alternative of using mb1->accept()?

    I don't know whether I explained the question well. Please feel free to ask if I did too bad.. Thanks in advance!
    Last edited by shijiaguo; 21st May 2013 at 19:05.

  2. #2
    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: QMessageBox question

    Connect to the accepted() signal from the dialog.
    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.


  3. #3

    Default Re: QMessageBox question

    Hi thanks so much wysota!
    Could you please explain a little bit more how to connect it? I'm pretty new to Qt. Thanks again!!


    Added after 37 minutes:


    I actually tried that. I did something like this:
    QObject::connect(*handle, SIGNAL(accepted()), okButton, SLOT(click())); //okButton is a QPushButton *
    But it still doesn't get out of the 1st exec() immediately.
    Last edited by shijiaguo; 21st May 2013 at 15:30.

  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: QMessageBox question

    You are supposed to put a log message in the slot connected to the accepted() signal, not click any buttons.
    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

    Default Re: QMessageBox question

    This won't get to my expected results. mb1 is dismissed (accepted) immediately after I call mb1->accept(), and the log message "messagebox dismissed by system\n" is immediately show. This is ok with the current system. However, it doesn't stop the exec() and do the 1st "messagebox finished\n" immediately. This is what I want it to do. Instead, it shows the 2nd "messagebox finished" first then the 1st. Maybe I should make this more clear since my logs of the 2 messagebox are different. I update the original post.


    Added after 42 minutes:


    I'm stuck here forever. Any idea will be really appreciated!
    Last edited by shijiaguo; 21st May 2013 at 16:30.

  6. #6

    Default Re: QMessageBox question

    I guess I naturally solved it. Instead of releasing the handle within showMessageBox(), I released it in main() and that immediately finishes exec(). Don't why this happens though.

Similar Threads

  1. QMessageBox
    By sonuani in forum Qt Programming
    Replies: 13
    Last Post: 2nd September 2009, 17:56
  2. QMessageBox
    By sonuani in forum Qt Programming
    Replies: 1
    Last Post: 31st March 2008, 08:27
  3. Re: Help on QMessageBox
    By arunvv in forum Newbie
    Replies: 2
    Last Post: 25th March 2008, 23:45
  4. how to use tr() for QMessageBox?
    By gfunk in forum Qt Programming
    Replies: 4
    Last Post: 17th November 2007, 11:30
  5. QMessageBox::information box question
    By bruccutler in forum Qt Programming
    Replies: 1
    Last Post: 17th June 2007, 17:30

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.