Qt Forum

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

    Call for Presentations - Qt World Summit

    Unsolved Qt Quick Controls 2 - How to grab the items contained in a TableView in images in order to print them?

    QML and Qt Quick
    qtquick2 printing printtopdf grab image
    1
    1
    200
    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.
    • jeanmilost
      jeanmilost last edited by

      I need to print the whole content of a TableView. How can I grab its items in images in order to print them?

      I tried the following code, but no way, this doesn't work:

      QList<QObject*> objects = m_pEngine->rootObjects();
      QQuickItem*     pItem   = qobject_cast<QQuickItem*>(objects[0]->findChild<QObject*>("gvMessageGrid"));
          
      if (pItem)
      {
          QImage pImage = pItem->grabToImage()->image();
          ...
      

      The above code always return an empty image.

      For info, here is the qml code for my TableView:

      /**
      * Grid view item
      */
      Component
      {
          id: itGridItem
              
          Item
          {
              width: gvMessageGrid.width
              height: itemTextID.height + 40
              
              Rectangle
              {
                  property int messageWidth: (gvMessageGrid.width / 2) - 50
              
                  id: itemRect
                  x: senderIsMyself ? 25 : gvMessageGrid.width - (25 + messageWidth)
                  y: 5
                  width: messageWidth
                  height: itemTextID.height + 20
                  color: senderIsMyself ? "#d5d5d5" : "#800b940e"
                  radius: 5
                  clip: true
              
                  Text
                  {
                      id: itemTextID
                      width: parent.width - 20
                      text: itemText
                      renderType: Text.NativeRendering
                      textFormat: Text.StyledText
                      wrapMode: Text.WordWrap
                      font.family: "Segoe UI Emoji"
                      font.pixelSize: 18
                      anchors.margins: 10
                      anchors.left: parent.left
                      anchors.top: parent.top
                      color: "#101010"
                  }
              }
          }
      }
              
      /**
      * Messages grid view
      */
      ListView
      {
          id: gvMessageGrid
          objectName: "gvMessageGrid"
          y: 0
          Layout.fillHeight: true
          flickableDirection: Flickable.VerticalFlick
          Layout.fillWidth: true
          Layout.alignment: Qt.AlignHCenter | Qt.AlignVCenter
          clip: true
          contentWidth: 700
          contentHeight: 300
          model: lmGridModel
          delegate: itGridItem
              
          ScrollBar.vertical: ScrollBar
          {
              visible: true
              minimumSize: 0.1
          }
      }        
      

      An important note: Please don't provide solutions involving Qt Quick Controls 1.x. I cannot use them, as they contain too many severe performance issues, and they aren't DPI aware. My company rejected them, so it's not a solution for me. Only Qt Quick Controls 2.0 are accepted.

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