Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Get QPixmap from Image Item in C++

    QML and Qt Quick
    3
    7
    2580
    Loading More Posts
    • 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.
    • Gianluca
      Gianluca last edited by

      Dear all,
      I want to pass an Image item to a C++ method and from that retrieve as QPixmap the image displayed. Is it possibile ?
      In pseudo-code, what I want to achieve is:
      in QML:
      @
      Image {
      id: image
      // can be remote url, qrc or local file
      source: "path/to/image"
      }

      MouseArea {
      onClicked: CppObject.postImage( image )
      }
      @
      and in the C++ implementation of postImage:
      @
      void CppObject::postImage( QQuickItem* image ) {
      QPixmap pix = image->howToRetrieveTheImageAsQPixmap();
      }
      @

      Thanks,
      Gianluca.

      1 Reply Last reply Reply Quote 0
      • JKSH
        JKSH Moderators last edited by

        Hi,

        See the "QPixmap documentation":http://doc.qt.io/qt-5/qpixmap.html#fromImage

        Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

        1 Reply Last reply Reply Quote 0
        • Gianluca
          Gianluca last edited by

          [quote author="JKSH" date="1418255486"]Hi,

          See the "QPixmap documentation":http://doc.qt.io/qt-5/qpixmap.html#fromImage[/quote]

          I read the documentation ... but the method you point it's not useful for me. I need to get a QPixmap from a Image (not QImage). Image item is a Qt Quick 2 component. It's different from QImage.

          1 Reply Last reply Reply Quote 0
          • JKSH
            JKSH Moderators last edited by

            [quote author="Gianluca" date="1418282896"]I read the documentation ... but the method you point it's not useful for me. I need to get a QPixmap from a Image (not QImage). Image item is a Qt Quick 2 component. It's different from QImage.[/quote]My apologies! I misread your post and thought you wanted to use QImage.

            I don't know of a way to extract the data from a QML Image object. The closest I can think of is QQuickView::grabWindow(), but that grabs everything visible, not just the image.

            If you don't get any answers here, try asking at the "Interest mailing list":http://lists.qt-project.org/mailman/listinfo/interest -- Qt engineers are active there (you'll need to subscribe first)

            Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

            1 Reply Last reply Reply Quote 0
            • U
              utcenter last edited by

              grabWindow() is quite clumsy, and while at this time there is no way to get a QPixmap from a QML Image, the other way around is possible - you can have a QPixmap inside a QML component which you can modify in C++ and display in QML using a custom image provider.

              Check this out: http://stackoverflow.com/questions/27429371/qml-and-c-image-interoperability

              1 Reply Last reply Reply Quote 0
              • Gianluca
                Gianluca last edited by

                [quote author="utcenter" date="1418322791"]
                Check this out: http://stackoverflow.com/questions/27429371/qml-and-c-image-interoperability[/quote]

                I cannot use that method because I'm writing a library, so the functionality that I need will be used by the users of the library not by myself.

                I think that for now the only possible solution is to pass the value of source property instead of passing the whole Item.

                But in this case the question change in:
                How can I create a QPixmap from "image://" and "qrc://" url schemas from C++ ?

                1 Reply Last reply Reply Quote 0
                • U
                  utcenter last edited by

                  bq. I cannot use that method because I’m writing a library, so the functionality that I need will be used by the users of the library not by myself.

                  You probably can put it into the library, i.e. the plugin, with:

                  @void QQmlExtensionPlugin::​initializeEngine(QQmlEngine * engine, const char * uri)@

                  Just register the image provider with the engine in that function.

                  There is nothing preventing you from creating a pixmap from a file in qrc, but it would be quite limiting only being able to use images packed in the resource file.

                  1 Reply Last reply Reply Quote 0
                  • First post
                    Last post