Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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



  • 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.