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

Can we draw QT image into QML ?



  • This post is deleted!

  • Moderators

    @TM9412
    QImageProvider is your friend. with it you can pass a QImage or a QPixmap to your qml-UI

    Good stackoverflow thread here: link



  • This post is deleted!

  • Moderators

    @TM9412
    You should at least try to do this on your own....

    //colorimageprovider.h
    #ifndef COLORIMAGEPROVIDER_H
    #define COLORIMAGEPROVIDER_H
    
    #include <QQuickImageProvider>
    
    class ColorImageProvider : public QQuickImageProvider
    {
    public:
        ColorImageProvider()
            : QQuickImageProvider(QQuickImageProvider::Pixmap)
        {
        }
    
        QPixmap requestPixmap(const QString &id, QSize *size, const QSize &requestedSize)
        {
            int width = 100;
            int height = 50;
    
            if (size)
                *size = QSize(width, height);
            QPixmap pixmap(requestedSize.width() > 0 ? requestedSize.width() : width,
                           requestedSize.height() > 0 ? requestedSize.height() : height);
            pixmap.fill(QColor(id).rgba());
    
            return pixmap;
        }
    
    };
    #endif // COLORIMAGEPROVIDER_H
    
    //main.cpp
    #include <QGuiApplication>
    #include <QQmlApplicationEngine>
    
    #include "colorimageprovider.h"
    
    int main(int argc, char *argv[])
    {
        QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    
        QGuiApplication app(argc, argv);
    
        QQmlApplicationEngine engine;
        engine.addImageProvider(QLatin1String("myProvider"), new ColorImageProvider);
    
        engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    
        if (engine.rootObjects().isEmpty())
            return -1;
    
        return app.exec();
    }
    
    
    //main.qml
    import QtQuick 2.9
    import QtQuick.Window 2.2
    
    Window {
        visible: true
        width: 640
        height: 480
        title: qsTr("Hello World")
    
    
        Image {
            id: myImage
            anchors.fill: parent
            source: "image://myProvider/red"
        }
    }
    


  • This post is deleted!

  • Moderators

    @TM9412
    hi,
    there should be no difference, in whether the Image -Object is framed by a Window- Object or Item-Object.

    Thats totoaly up to you.

    //main.qml
    import QtQuick 2.9
    import QtQuick.Window 2.2
    
    Window {
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")
    
    Item{
        anchors.fill: parent
    
        Image {
            id: myImage
            anchors.fill: parent
            source: "image://myProvider/red"
        }
    }
    }
    


  • This post is deleted!

Log in to reply