Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Can we draw QT image into QML ?

Can we draw QT image into QML ?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
7 Posts 2 Posters 2.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    TM9412
    wrote on last edited by TM9412
    #1
    This post is deleted!
    J.HilkJ 1 Reply Last reply
    0
    • T TM9412

      This post is deleted!

      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by
      #2

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

      Good stackoverflow thread here: link


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      1 Reply Last reply
      1
      • T Offline
        T Offline
        TM9412
        wrote on last edited by
        #3
        This post is deleted!
        J.HilkJ 1 Reply Last reply
        0
        • T TM9412

          This post is deleted!

          J.HilkJ Offline
          J.HilkJ Offline
          J.Hilk
          Moderators
          wrote on last edited by
          #4

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

          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          1 Reply Last reply
          1
          • T Offline
            T Offline
            TM9412
            wrote on last edited by TM9412
            #5
            This post is deleted!
            J.HilkJ 1 Reply Last reply
            0
            • T TM9412

              This post is deleted!

              J.HilkJ Offline
              J.HilkJ Offline
              J.Hilk
              Moderators
              wrote on last edited by J.Hilk
              #6

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

              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

              1 Reply Last reply
              0
              • T Offline
                T Offline
                TM9412
                wrote on last edited by
                #7
                This post is deleted!
                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved