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. Qt Quick Controls 2 - How to grab the items contained in a TableView in images in order to print them?

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

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
qtquick2printingprinttopdfgrabimage
1 Posts 1 Posters 421 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.
  • J Offline
    J Offline
    jeanmilost
    wrote on 1 Apr 2020, 14:25 last edited by
    #1

    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
    0

    1/1

    1 Apr 2020, 14:25

    • Login

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