[Interest] promoting a QGuiApplication to a QApplication?

René J.V. Bertin rjvbertin at gmail.com
Wed Jul 11 16:48:31 CEST 2018


On Wednesday July 11 2018 17:20:51 Konstantin Tokarev wrote:

Hi

> If application object is created as QGuiApplication, you obviously cannot cast it to QApplication.

Maybe for casting, but 

> If you want to do something only when application object is created as QApplication (or its subclass), use qApp pointer

Well, that's not enough. qApp exists and isn't NULL in other Q*Applications because qApp is just a cast of QCoreApplication::instance().

A bit of context may help. 

The plugin I'm tinkering with is KDE's qqc2-desktop-style, which aims to implement a QtQuickControls2 style that follows the user's current desktop (= widget) style. In its current implementation it does this by querying `qApp->style()` every time the style must be called upon. When you select that style in a basic QQC2 application (say, the gallery demo) it will crash horribly.

My tinkering is under review here: https://phabricator.kde.org/D14000 . Instead of using qApp->style() directly I use QStyleFactory to create a style instance when qApp->style() returns NULL, and cache it because qApp->setStyle(newStyle) would also crash.

This works fine, as long as the style doesn't do anything that requires a QApplication and will crash (or abort) otherwise.
The only guarantee I can think of is would be promoting a QGuiApplication instance to QApplication at runtime, in the plugin.

R.



More information about the Interest mailing list