QML Image disply without window
-
Ok Im new to qml and I found this example code to run:
import QtQuick 2.12 Image { id: root source: "images/background.png" }
However If I run this nothing shows up.
I created an empty Qt Quick app with creator and it looks like it needs a window.
So this worked well:
import QtQuick 2.14 import QtQuick.Window 2.14 Window { visible: true width: 640 height: 480 Image { id: root source: "images/background.png" } }
So short stupid question. Can Image get displayed without the window?
-
I just made a empty Qt Quick project and hit build and run in debug.
I think the qml gets run with its coresponding main.cpp
#include <QGuiApplication> #include <QQmlApplicationEngine> int main(int argc, char *argv[]) { QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); QGuiApplication app(argc, argv); QQmlApplicationEngine engine; const QUrl url(QStringLiteral("qrc:/main.qml")); QObject::connect(&engine, &QQmlApplicationEngine::objectCreated, &app, [url](QObject *obj, const QUrl &objUrl) { if (!obj && url == objUrl) QCoreApplication::exit(-1); }, Qt::QueuedConnection); engine.load(url); return app.exec(); }
-
@sandro4912 said in QML Image disply without window:
import QtQuick 2.12
Image {
id: root
source: "images/background.png"
}That was not the question.
The Question is. Can this work without window like this:
import QtQuick 2.12 Image { id: root source: "images/background.png" }
-
@sandro4912
AFAIKQQmlApplicationEngine
needs a Window or similar top level QML componentif you use a QQuickWidget / QQuickView you may get away with an Item/Image as root element
-
@J-Hilk said in QML Image disply without window:
QQuickWidget
I modified my main to this:
#include <QApplication> #include <QQuickWidget> int main(int argc, char *argv[]) { QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); QApplication app(argc, argv); auto view = new QQuickWidget; view->setSource(QStringLiteral("qrc:/main.qml")); view->show(); return app.exec(); }
Now I wonder whats the difference between
QQuickWidget
andQQmlApplicationEngine
. Why not do everything with theQQuickWidget
? -
Hi,
Because you might want to only use QML. QQuickWidget adds the widget module as dependency which might not be what you want.