Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call QMessageBox Static API outside of a QWidget Sub-Class

I have a utility class in my Qt GUI application. However, in my convenience class I wanted to call a QMessageBox::critical(), warning(), etc. The class isn't a QWidget, and therefore I cannot pass this as the parent. My class is subclassed from QObject, however, so it can run things such as signals and slots. So to work around this--if it's possible to--should I maybe look at the property API instead of using the Static API?

Class declaration:

class NetworkManager : public QObject

And here's an example of a Static API call that fails:

QMessageBox::critical(this, tr("Network"), tr("Unable to connect to host.\n"),
QMessageBox::Ok | QMessageBox::Discard);

So, if I were to build a Property based API message box, would it be possible to call it in a QObject somehow? I haven't really used the Property Based API, but I understand from the documentation that it seems to use an event loop (i.e. exec()).

like image 350
ContingencyCoder Avatar asked Mar 19 '13 15:03

ContingencyCoder


1 Answers

Just pass NULL for the first parameter:

QMessageBox::critical(NULL, QObject::tr("Error"), QObject::tr("..."));
like image 84
Nathan Osman Avatar answered Oct 26 '22 23:10

Nathan Osman