Pim
Pim

Reputation: 433

Closing QMessageBox on timer, setText not showing

The following piece of code closes my QMessageBox after 2 secs. But my text shows when the box is closing, It flashes really quick before the box closes. What is going on here?

 QMessageBox *msgBox = new QMessageBox();
 msgBox->setText("Coördinate is being created, please wait...");
 msgBox->show();
 QTimer::singleShot(2000, msgBox, SLOT(hide()));

enter image description here

This shows, and then just before closing I can see the text.

update

Working in a single thread program: Method WriteMultipleACLCommands() is taking up a lot of time. Maybe thats the issue?

  QMessageBox *msgBox = new QMessageBox();
  msgBox->setText("Coördinate is being created, please wait...");
  msgBox->show();
  QTimer::singleShot(2000, msgBox, SLOT(hide()));
  singleton_SerialPortManager->WriteMultipleACLCommands();
  //function writes a few bytes onto a serial connection

Upvotes: 0

Views: 1920

Answers (2)

user362515
user362515

Reputation: 917

After the update,

Of course it is an issue if you don't return from the calling function right away - you are blocking the event loop, hence updates to all widgets!

Possible Solution

You can make WriteMultipleACLCommands Q_INVOKABLE (or a slot) and invoke it as Qt::QueuedConnection:

QMetaObject::invokeMethod(singleton_SerialPortManager, "WriteMultipleACLCommands", Qt::QueuedConnection);

This way you just post an event to the events queue and return right away. After that the message box will receive an update, then, at some point WriteMultipleACLCommands will be called as well.

Upvotes: 1

0rko
0rko

Reputation: 168

Your Code is fine, at least the part you are showing. I tested it myself and it works without any problems. But keep in mind, that closing and hiding the dialog are two different things. You just hide the Window. The Window will still exist in memory. Maybe you want to call the "close slot" in your timer and set the windows attribute to "delete on close":

QMessageBox *msgBox = new QMessageBox();
msgBox->setText("Coördinate is being created, please wait...");
msgBox->show();
msgBox->setAttribute(Qt::WA_DeleteOnClose);
QTimer::singleShot(2000, msgBox, SLOT(close()));

If this isn't the cause of the effect you described, you have to give some more information.

Upvotes: 0

Related Questions