PDA

View Full Version : Just launch QMessageBox and then continue with following code?



hakermania
9th January 2011, 19:29
Hello.

This is the code:


QMessageBox msgBox;
msgBox.setText("Please wait to connect to the server.");
msgBox.setWindowTitle("Checking for update.");
msgBox.exec();

This executes the msgBox and waits for the user to click OK or [X] to the messagebox dialog. What I need is, after the .exec the program to continue with the following code (for instance to start checking for the update) and perform the check:


if(msgBox.isVisible())
msgBox.close();

before displaying another message letting the user know if there is or not update.

How can you just launch a MessageBox?
(for anybody familiar to bash scripting it is similar to the symbol '&' after calling an application)

high_flyer
9th January 2011, 20:49
Did you try show()?
Better yet, use setWindowModality().

hakermania
14th January 2011, 19:42
Hello and thanks for the answer. Can you explain me a bit how setWindowModality works actually? I tried this:

QMessageBox msgBox;
msgBox.setText("Please wait to connect to the server.");
msgBox.setWindowTitle("Checking for update.");
msgBox.setWindowModality(Qt::WindowModal);
msgBox.exec();
but it doesn't work. It works exactly like the .exec(); option without setting any modality.

Can you help a bit? Thanks a lot!:)

Diath
14th January 2011, 20:54
Not sure if I got you correct, but you have to use QThread for that.

hakermania
14th January 2011, 21:26
Not sure if I got you correct, but you have to use QThread for that.

Hey, thx for the extra answer. Can you guide me a bit? :)

boudie
14th January 2011, 21:35
You dont't have to use threads.

Do this:


msgBox.show();


instead of


msgBox.exec();

hakermania
15th January 2011, 20:59
.show(); does move on with the following code but the program stucks (without displaying the Msgbox) till the call of .close(); which closes it and opens up another messagebox with .exec();

I am quite confused, I already have taken a lot of solution on this issue :/

.:saeed:.
15th January 2011, 22:13
I think QProgressDialog is the best choice for your request
Your sign is correct "When you 're trying to help somebody in the newbie section, don't forget that he is a newbie. Be specific and give examples. "
but I think a newbie has to learn how to use Qt Assistant. So please read its complete def about QProgressDialog and ask your Questions,
But don't forget this note:
If you can't predict the progress of your situation , ( Like connecting to server ) you can use this code to change the behavior of your progress dialog


QProgressDialog *dlg = new QProgressDialog;
dlg->setRange( 0, 0 );


I wish my English writing wasn't horrible :p

hakermania
22nd January 2011, 13:56
Thanks. I am trying to create a new QDialog and have the progressbar there, but I have the same problem with the show and exec.

stampede
22nd January 2011, 14:22
.show(); does move on with the following code but the program stucks (without displaying the Msgbox)

The difference is that exec() shows the window and blocks untill it's closed, where show() just posts show event to widget and continues without blocking.

consider the following examples:
1)

QMessageBox box;
box.show();
while(1);
2)

QMessageBox box;
box.exec();
while(1);
3)

QMessageBox box;
box.show();
qApp->processEvents();
while(1);

All of them will eventualy loop forever, but thats not important right now.
In 1) you probably wont see the message box at all, because the "show" event will not be delivered to message box, program enters while(1) loop and happily loops.
In 2) message box will be displayed and program will wait untill you'll close it or click the default "ok" button.
3) will render the box properly, but you will be unable to do anything, because the while(1) loop will start running.
Its because processing the events will show your message box before moving on.
I hope its clear enough.

john_god
22nd January 2011, 14:23
Probably your code is inside a function, and msgBox gets out of scope, use a pointer:


QMessageBox *msgBox = new QMessageBox;
msgBox->setText("Please wait to connect to the server.");
msgBox->setWindowTitle("Checking for update.");
msgBox->setWindowModality(Qt::NonModal);
msgBox->show();

Thsi code is just a example. The best option, probably, would be to declare msgBox inside of a class, for example, because if you keep calling this function, you will probably have a memory leak.

Stef
22nd January 2011, 19:56
You won't have a memory leak if you set the attribute such that the object is deleted once the messagebox is closed. You can do this by setting:


msgBox->setAttribute(Qt::WA_DeleteOnClose);

hakermania
23rd January 2011, 11:28
Hey, thank you all. It finally worked. This is what I did:

QMessageBox *msgBox1 = new QMessageBox;
//here button declarations
msgBox1->setWindowModality(Qt::NonModal);
QCoreApplication::processEvents();
msgBox1->show();
QCoreApplication::processEvents();
Thank you all :)
On last problem:
After this first "Please Wait" button, I want to close it and open the next one, which informs the user about update or not. The thing is, that when I call
if(msgbox->isEnabled())
msgbox->close();
it just deletes all the "widgets" of the msgBox and it let it empty. What should I call?

EDIT:
I was wrong, the msgBox when is shown IS EMPTY (nothing is shown, even if I have call setText() )
When another button is shown and I call msgbox->close(), the text is shown for 0.00001 secs and then it disappears, as it should...
What's wrong?

hakermania
26th January 2011, 16:11
Well, I've placed everywhere qApp->processEvents(); and I have
msgBox1->setAttribute(Qt::WA_DeleteOnClose);
msgBox1->setWindowModality(Qt::NonModal);

but now, sometimes it does show up normally, sometimes it is too slow and it only updates when it is to close to show the next messagebox...Quite weird.... ANy other who want to help me a bit? I think I am close....