PDA

View Full Version : QMessageBox question



shijiaguo
21st May 2013, 14:35
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:



void showMessageBox(QMessageBox **handle, QString& title)
{
QMessageBox mb = new QMessageBox(title, textBody, QMessageBox::Warning, QMessageBox::Ok);
*handle = &mb;
<Some message box format settings here>
exec();
printf("%s finished\n", title);
if (handle != NULL)
*handle = NULL;
}

void main(void)
{
QMessageBox *mb1;
showMessageBox(&mb1, "mb1"); //call exec() 1st time
if (power_supply_in)
{
mb1->accept(); //mb1 disappears on the screen
printf("mb1 dismissed by system\n");
QMessageBox *mb2;
showMessageBox(&mb2, "mb2"); //call exec() 2nd time
}
}


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!

wysota
21st May 2013, 14:47
Connect to the accepted() signal from the dialog.

shijiaguo
21st May 2013, 15:30
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.

wysota
21st May 2013, 15:39
You are supposed to put a log message in the slot connected to the accepted() signal, not click any buttons.

shijiaguo
21st May 2013, 16:30
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!

shijiaguo
21st May 2013, 19:06
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.