[SOLVED] Which method of the main Widget returns the QApplication instance?
-
I cannot figure out which method of eg. QWidget one can use to get hold of the QApplication's instance. Is there one?
Otherwise, should I pass the QApplication as parent of the main widget?Thanks.
B. -
Thanks guys for your answers :)
-
[quote author="octal" date="1314826374"]If you want to get a pointer to your QApplication, you can either use :
- qApp global pointer
- QApplication::instance() static function[/quote]
I just want to add that QApplication does not have an instance() method, its base class, QCoreApplication, has. This means that a QCoreApplication* is returned, which has to be cast properly.
@
QApplication* application = static_cast<QApplication *>(QApplication::instance());
@ -
[quote author="octal" date="1314861746"]Yes, qApp performs a cast :
@#define qApp (static_cast<QApplication *>(QCoreApplication::instance()))@
However, the doc states :
bq. Returns a pointer to the application's QCoreApplication (or QApplication) instance.[/quote]
Did you notice that in QCoreApplication, the qApp macro is also defined (line 69 in 4.7.3)?
@
#define qApp QCoreApplication::instance()
@So, if you #include <QCoreApplication>, you will end up with the define above, but if you #include <QApplication> you will get the version you quote.
-
What worked for me was using pythonic @QtCore.QCoreApplication.instance()@ because @QtGui.qApp@ returned some QApplication object, but it wasn't the instance I created (it didn't have the attributes I had given it).
Cheers :)
-
PS: it's my opinion that it would make sense if QWidgets had a getApplication() method.
-
Well you can't run more than one QApplication, and Widgets don't exist without a QApplication right? So in a sense the QApplication is a "parent" of all widgets. It's seems more object-oriented to me. It's my logic, but I don't claim it's universal :)
-
The object oriented pattern is called "singleton". And it's object oriented design to get that singleton instance by means of a static method of that class, and not by some unrelated other class. You will need the Q(Core)Application method anyways, as you can have an instance of that without any widgets. So no, an getApplication() method for QWidget, does not make any sense.