Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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?



  • How do you execute that code?



  • 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();
    }
    


  • This post is deleted!


  • @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"
    }
    

  • Moderators

    @sandro4912
    AFAIK QQmlApplicationEngine needs a Window or similar top level QML component

    if 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 and QQmlApplicationEngine. Why not do everything with the QQuickWidget?


  • Lifetime Qt Champion

    Hi,

    Because you might want to only use QML. QQuickWidget adds the widget module as dependency which might not be what you want.


Log in to reply