PDA

View Full Version : Non-Blocking QMessageBox



enricong
21st July 2011, 21:01
I'm trying to display a message box but I do not want it to block, How can I accomplish this.



QMessageBox msgBox(this);
msgBox.setWindowTitle("Loading ....");
msgBox.setText("Reading Files, please wait ...");
msgBox.open();
// Run Stuff
msgBox.close();


If I use open() or show() the messagebox never shows up. If I use exec(), it does show up, but it blocks.

The only way I got something like this to works is if I use ProgressDialog but then I need to use the progress bar. Unfortunately the task am doing is a shell call which provides no status.

SixDegrees
21st July 2011, 21:46
QMessageBox is explicitly modal. If you want a dialog that doesn't block, use QDialog.

I question whether you really want this. Your tells the user that files are being loaded, then explicitly says "please wait". Apparently, you don't really want them doing anything until those files are read.

If you use a QDialog and allow the user to fiddle with the rest of your interface, make sure you disable any controls that rely on the files being completely read until after IO completes.

Santosh Reddy
22nd July 2011, 02:38
Just do the following modification, you should be all set. You should be calling show (instead of open), also required to modify the modality of the message box before showing it


QMessageBox msgBox(this);
msgBox.setWindowTitle("Loading ....");
msgBox.setText("Reading Files, please wait ...");
msgBox.setWindowModality(Qt::NonModal);
//msgBox.open();
msgBox.show();
// Run Stuff
msgBox.close();

enricong
25th July 2011, 15:34
I have tried using "show()" however no window shows up and the code continues.

Only "exec()" causes a window to show up, but it blocks.

Santosh Reddy
25th July 2011, 16:12
Make sure QMessageBox has a widget (terminal widget) as parent. Just try using the above code in a simple project, it should work, then compare it with what you do in your project.

Lesiok
25th July 2011, 16:30
I have tried using "show()" however no window shows up and the code continues.

Only "exec()" causes a window to show up, but it blocks.
Problem is line 7 (Run Stuff). I think that this line blocks event que and all graphics processes needs working event que. Try to add after show() this line :
QCoreApplication::processEvents();
Remember that this is workaround not the solution.

enricong
25th July 2011, 17:29
Yes, this is the problem.
Adding that line allowed the window to show up, although the text in the window did not show up.

"Run Stuff" is actually a shell call which provides no progress feedback.
Perhaps I need to run it in its own thread.

high_flyer
25th July 2011, 18:00
Adding that line allowed the window to show up, although the text in the window did not show up.
That is the same issue as you had before - processEvents() allows some events to be handled (like the MessageBox to be shown) but your "Run Stuff" then stopped it again, before the text could be drawn.

Perhaps I need to run it in its own thread.
That would probably be better in this case.
See QConcurrent, it sounds like its perfect for this case.

enricong
25th July 2011, 23:31
Thanks, I'll have to try to figure out how to modify my program to work with "QtConcurrent"

Lesiok
26th July 2011, 10:14
Yes, this is the problem.
Adding that line allowed the window to show up, although the text in the window did not show up.

"Run Stuff" is actually a shell call which provides no progress feedback.
Perhaps I need to run it in its own thread.

On X-11 platform You need to call processEvents() 4-5 times. On Windows one call is enough.