Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How QImage from c++ use in QML
Forum Updated to NodeBB v4.3 + New Features

How QImage from c++ use in QML

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 4 Posters 4.5k Views 1 Watching
  • 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.
  • M Offline
    M Offline
    Mihaill
    wrote on 13 Feb 2022, 19:09 last edited by
    #1

    Hi !
    How QImage from c++ use in QML?
    I have QImage

    QImage m_iRImage;
    

    and Q_PROPERTY

    Q_PROPERTY(QImage iRImage MEMBER m_iRImage NOTIFY currentIRImageChanged)
    

    and Image in QML (AppCore + my c++ class)

    import AppCore 1.0
    
    
                Image {
                    source: AppCore.iRImage
                    fillMode: Image.PreserveAspectFit
                }
    
    1 Reply Last reply
    0
    • S Offline
      S Offline
      sierdzio
      Moderators
      wrote on 14 Feb 2022, 06:16 last edited by
      #2

      To use a QImage in a QML image, you need to use an image provider https://doc.qt.io/qt-5/qquickimageprovider.html.

      Or, simpler way - return a path to your QImage in the property (so change your property into a QString), then QML will understand it.

      (Z(:^

      1 Reply Last reply
      0
      • M Offline
        M Offline
        Mihaill
        wrote on 14 Feb 2022, 06:45 last edited by
        #3

        Please, give me minimal work example with QQuickImageProvider.
        And why here inherited from QQuickImageProvider, why not create QQuickImageProvider object?

        J 1 Reply Last reply 14 Feb 2022, 07:14
        0
        • S Offline
          S Offline
          sierdzio
          Moderators
          wrote on 14 Feb 2022, 06:49 last edited by
          #4

          The documentation provides an example. Not sure if I can conjure up anything better :-)

          And why here inherited from QQuickImageProvider, why not create QQuickImageProvider object?

          It's inherited to provide custom functionality in requestPixmap. That's what you should use, too, to provide your image.

          (Z(:^

          1 Reply Last reply
          1
          • M Mihaill
            14 Feb 2022, 06:45

            Please, give me minimal work example with QQuickImageProvider.
            And why here inherited from QQuickImageProvider, why not create QQuickImageProvider object?

            J Offline
            J Offline
            J.Hilk
            Moderators
            wrote on 14 Feb 2022, 07:14 last edited by
            #5

            @Mihaill here's an example from my repository
            https://github.com/DeiVadder/Sandepile-Challenge-QML

            it's not a pure image provide example, but that is part of the "Sand Pile Challenge", should be minimal enough :D


            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
            3
            • M Offline
              M Offline
              Mihaill
              wrote on 14 Feb 2022, 07:49 last edited by Mihaill
              #6

              I don't understand how QQuickImageProvider is passed to QML. And how to get this picture in QML?
              I find example ImageProvider, but he dont work.

              K 1 Reply Last reply 14 Feb 2022, 08:18
              0
              • M Mihaill
                14 Feb 2022, 07:49

                I don't understand how QQuickImageProvider is passed to QML. And how to get this picture in QML?
                I find example ImageProvider, but he dont work.

                K Offline
                K Offline
                KroMignon
                wrote on 14 Feb 2022, 08:18 last edited by KroMignon
                #7

                @Mihaill said in How QImage from c++ use in QML:

                I don't understand how QQuickImageProvider is passed to QML. And how to get this picture in QML?

                You have register it into QML context:

                    QQuickView view;
                    QQmlEngine *engine = view.engine();
                    // ==> image provider will be accessible with name provided (e.g. testProvider)
                    engine->addImageProvider(QLatin1String("testProvider"), new MyImageProvider);
                

                Then in QML:

                Image { source: "image://testProvider/Image.png" }
                

                Take a look at documentation => https://doc.qt.io/qt-5/qquickimageprovider.html#an-example

                It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                1 Reply Last reply
                1
                • M Offline
                  M Offline
                  Mihaill
                  wrote on 14 Feb 2022, 13:03 last edited by
                  #8

                  It is more simple and work

                  QUrl imageToUrl(const QImage& image)
                  {
                      QByteArray byteArray;
                      QBuffer buffer(&byteArray);
                      buffer.open(QIODevice::WriteOnly);
                      image.save(&buffer, "png");
                      QString base64 = QString::fromUtf8(byteArray.toBase64());
                      return QString("data:image/png;base64,") + base64;
                  }
                  
                  1 Reply Last reply
                  3

                  1/8

                  13 Feb 2022, 19:09

                  • Login

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