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. Get QPixmap from Image Item in C++
QtWS25 Last Chance

Get QPixmap from Image Item in C++

Scheduled Pinned Locked Moved QML and Qt Quick
7 Posts 3 Posters 3.1k 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.
  • GianlucaG Offline
    GianlucaG Offline
    Gianluca
    wrote on last edited by
    #1

    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
    0
    • JKSHJ Offline
      JKSHJ Offline
      JKSH
      Moderators
      wrote on last edited by
      #2

      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
      0
      • GianlucaG Offline
        GianlucaG Offline
        Gianluca
        wrote on last edited by
        #3

        [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
        0
        • JKSHJ Offline
          JKSHJ Offline
          JKSH
          Moderators
          wrote on last edited by
          #4

          [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
          0
          • U Offline
            U Offline
            utcenter
            wrote on last edited by
            #5

            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
            0
            • GianlucaG Offline
              GianlucaG Offline
              Gianluca
              wrote on last edited by
              #6

              [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
              0
              • U Offline
                U Offline
                utcenter
                wrote on last edited by
                #7

                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
                0

                • Login

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