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. Stronger way to render a QQuickItem (WebView) into an Image

Stronger way to render a QQuickItem (WebView) into an Image

Scheduled Pinned Locked Moved QML and Qt Quick
6 Posts 2 Posters 2.6k 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.
  • M Offline
    M Offline
    mperrinel
    wrote on last edited by
    #1

    Hi,

    Using the WebKit version of the Qt 5.4.0, I need to be able to print the content of a WebView :

    MenuItem {
    text: qsTr("&Print")
    onTriggered: {
    qmlPrinter.print(webView)
    }
    }

    WebView {
    id: webView
    objectName: "view"
    anchors.fill: parent
    url: "myHtmlPage.htm"
    }

    Because I can't see any answer from the QML part, I have tried with the C++ part:

    QmlItemPrinter::print(const QVariant &qmlItem)
    {
    QQuickItem *item = qobject_cast<QQuickItem >( qmlItem.value<QObject>() );
    }

    By googling, I have already tried different solution:

    1. The GrabWindow: -> Only works for the visible part of the WebView. But My webView have a contentHeigh of 23000.

    2. The grabToImage + ShaderEffectSource -> Image have the good size, but its contents is like the content of the resized grabWindow:
      ShaderEffectSource {
      id: webWiewShader
      height: webView.contentHeight
      width: webView.contentWidth
      live: false
      sourceItem: webView
      }

    3. The QQuickPaintedImage: Yes but how can I implement its paint method ?

    4. The QSGTextureProvider seems to not work with big WebView content:
      QSGTextureProvider *textureProvider = item->textureProvider();
      QSGTexture *texture = textureProvider->texture();
      texture->bind();
      GLint internalFormat;
      glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_COMPONENTS, &internalFormat); // get internal format type of GL texture
      QImage image(texture->textureSize(), QImage::Format_RGBX8888);
      glGetTexImage(GL_TEXTURE_2D, 0, internalFormat, GL_UNSIGNED_BYTE, image.bits());
      image.save("D:/myHtmlImage.jpg");

    5. The QGraphicsObject: It seems that the WebView cannot be cast to the QGraphicsObject.

    My goal is to print the html content. I thank that a first way was to create a QImage from the WebView but maybe there is a simplest way.

    Does anyone have a suggestion ?

    Thanks in advance,

    1 Reply Last reply
    1
    • J Offline
      J Offline
      jalomic
      wrote on last edited by jalomic
      #2

      You can cast your webview to QQuickPaintedItem ( because QQuickWebView iherits it), it has paint(QPainter *) method. Just replace QPainter and call this function !

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mperrinel
        wrote on last edited by
        #3

        Hi Jalomic and thanks for you reply.
        I have tried your way but without sucess.

        1. The webview cannot be cast to QQuickPaintedItem
        2. The webvies can be cast to QQuickWebView but:
        • I need then to include webkit-private and qquick-private to my .pro
        • the QQuickWebView inherit from QQuickFlickable then QQuickItem but no QQuickPaintedItem
        • There is no paint method for QQuickWebView
        J 2 Replies Last reply
        0
        • M mperrinel

          Hi Jalomic and thanks for you reply.
          I have tried your way but without sucess.

          1. The webview cannot be cast to QQuickPaintedItem
          2. The webvies can be cast to QQuickWebView but:
          • I need then to include webkit-private and qquick-private to my .pro
          • the QQuickWebView inherit from QQuickFlickable then QQuickItem but no QQuickPaintedItem
          • There is no paint method for QQuickWebView
          J Offline
          J Offline
          jalomic
          wrote on last edited by
          #4

          @mperrinel Oh sorry. I used https://github.com/mardy/qt5webkit1 =) and forgot about webkit2

          1 Reply Last reply
          0
          • M mperrinel

            Hi Jalomic and thanks for you reply.
            I have tried your way but without sucess.

            1. The webview cannot be cast to QQuickPaintedItem
            2. The webvies can be cast to QQuickWebView but:
            • I need then to include webkit-private and qquick-private to my .pro
            • the QQuickWebView inherit from QQuickFlickable then QQuickItem but no QQuickPaintedItem
            • There is no paint method for QQuickWebView
            J Offline
            J Offline
            jalomic
            wrote on last edited by
            #5

            @mperrinel It is very ruefully :(
            http://www.kdab.com/creating-pdf-qtquick-2-scene-slideviewer/

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mperrinel
              wrote on last edited by
              #6

              Thanks for the link. I have read it.

              It's very nice and can probably answer most of the case. But because it only works with a subset of QtQuickItem like image or text, I think that will not be enough for the webview. The biggest problem of the webview is it content height that can be very huge.
              The grabImage was a nice way, but I was blocked by a problem of memory. only about the first 2000 height was painted...

              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