How to preserve strokes after canvas resized



  • When the canvas resized, strokes will be clear. How to preserve them when adjust size of the canvas?
    (the canvas fill the window and the window can be resized by user)



  • @rock-wang hi,
    Could you plz show your code exemple ?
    I binded my canvas size to my windows size and when i'm resizing everything is preserved for me



  • import QtQuick 2.9
    import QtQuick.Window 2.2
    import QtQuick.Controls 1.5
    import QtQuick.Layouts 1.3
    import QtQuick.Controls.Styles.Desktop 1.0
    import QtQuick.Extras 1.4
    
    Window {
        visible: true
        width: 800
        height: 600
        //flags:Qt.FramelessWindowHint
        title: qsTr("优能视频客户端")
    
        ColumnLayout{
            id: mainLayout
            anchors.fill: parent
            Rectangle{
                width: 30
                height: 30
                color:'gray'
                Layout.alignment: Qt.AlignLeft | Qt.AlignTop
                Layout.fillWidth: true
                RowLayout {
                    id: rowLayout
                    anchors.fill: parent
                    Image {
                        id: imgIcon
                        sourceSize.height: 16
                        sourceSize.width: 16
                        fillMode: Image.Stretch
                        Layout.alignment: Qt.AlignLeft | Qt.AlignVCenter
                        source: "qrc:/images/Logo.png"
                    }
                    Text {
                        text: "优能视频客户端"
                        Layout.fillWidth: true
                    }
    
                    Image {
                        id: imgMin
                        sourceSize.height: 16
                        sourceSize.width: 16
                        fillMode: Image.Stretch
                        Layout.alignment: Qt.AlignLeft | Qt.AlignVCenter
                        source: "images/minimizeNormal.png"
                    }
    
                    Image {
                        id: imgMax
                        sourceSize.height: 16
                        sourceSize.width: 16
                        fillMode: Image.Stretch
                        Layout.alignment: Qt.AlignLeft | Qt.AlignVCenter
                        source: "images/max_2.png"
                    }
    
                    Image {
                        id: imgClose
                        sourceSize.height: 16
                        sourceSize.width: 16
                        fillMode: Image.Stretch
                        Layout.alignment: Qt.AlignLeft | Qt.AlignVCenter
                        source: "images/closeNormal.png"
                    }
                }
            }
    
            Canvas{
                property real lastX
                property real lastY
                id: canvas
                Layout.fillHeight: true
                Layout.fillWidth: true
                onPaint: {
                    var ctx = this.getContext("2d")
                    ctx.lineWidth = 5.0
                    ctx.strokeStyle = 'red'
                    ctx.beginPath()
                    ctx.moveTo(lastX, lastY)
                    lastX = area.mouseX
                    lastY = area.mouseY
                    ctx.lineTo(lastX, lastY)
                    ctx.stroke()
                }
                MouseArea{
                    id:area
                    anchors.fill: parent
                    onPressed: {
                        canvas.lastX = mouseX
                        canvas.lastY  =mouseY
                    }
                    onPositionChanged: {
                        canvas.requestPaint()
                    }
                }
            }
    
        }
    }
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.