What is the difference between QQmlApplicationEngine and QQuickView?
-
I'm using
QQmlApplicationEngine
as follows:QGuiApplication app(argc, argv); QQmlApplicationEngine engine; engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); app.exec();
But now I want to enable multisampling for my app, and
QQmlApplicationEngine
doesn't seem to have asetFormat
method for enabling multisampling.I found a way to do it with a
QQmlApplicationEngine
in a forum:QQuickWindow* window = (QQuickWindow*) engine.rootObjects().first(); QSurfaceFormat format; format.setSamples(16); window->setFormat(format)
But it relies on the first root object of the engine being a
QQuickWindow
, which is not documented in Qt docs. So I don't want to use that technique.Another way would be to skip
QQmlApplicationEngine
and create aQQuickView
instead. This does have asetFormat
method letting me enable multisampling, but I'm wondering, am I losing anything by switching fromQQmlApplicationEngine
toQQuickView
?In other words, what are the differences between these two classes?
One difference I found is this (from here):
Unlike QQuickView, QQmlApplicationEngine does not automatically create a root window. If you are using visual items from Qt Quick, you will need to place them inside of a Window.
This particular difference doesn't matter to me.
Any other differences?
-
@Stefan-Monov76
The basic difference isQQmlApplicationEngine
can only load QML files which have root component asWindow
orApplicationWindow
whileQQuickView
loads any QML type which inheritsItem
.But it relies on the first root object of the engine being a QQuickWindow, which is not documented in Qt docs. So I don't want to use that technique.
If you use
QQmlApplicationEngine
you have to useWindow
orApplicationWindow
which instantiatesQQuickWindow
.
So AFAIK the code you posted is the only way to set the samples. -
@Stefan-Monov76 As the docs say
QQuickView
is convenience subclass ofQQuickWindow
. -
QQmlApplicationEngine
is a convenience class introduced in Qt 5.1, with several advantages overQQuickView
:- Can also be used for non-graphical purposes (so whether the root object is a
QQuickView
depends on the QML you're loading). - Automates a bunch of things (
Qt.quit()
, translations, file selectors) - Allows subclasses like
ApplicationWindow
to be used as root item (whereas you would have aWindow
nested in aWindow
when using aQQuickView
, I'm not sure what it does in that case)
While technically redundant, I think it was a welcome addition since it saves a lot of boilerplate code.
- Can also be used for non-graphical purposes (so whether the root object is a
-
@thorbjorn : Thanks! Very useful answer (unlike the docs, which are really lacking on this :) ). I'll use the
QQmlApplicationEngine
way.