Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-137119

QML Canvas is not repainted if size changes while not visible

    XMLWordPrintable

Details

    • Bug
    • Resolution: Unresolved
    • P2: Important
    • None
    • 6.8.1
    • Quick: Other
    • None
    • Tested that the behavior is the same with 6.5.3 and 6.8.1 (on macOS).
    • macOS

    Description

      If the size of Canvas changes while the Canvas is not visible, we do not get a call to onPaint(). If the Canvas is later made visible, the content looks stretched.

      Example to see the effect.

      import QtQuick
      import QtQuick.Controls.Basic
      
      Window {
          width: 640
          height: 480
          visible: true
          title: qsTr("Canvas repainting")
      
          Rectangle {
              anchors.fill: mycanvas
              color: "grey"
          }
      
          Canvas {
              id: mycanvas
              anchors.centerIn: parent
              width: 100
              height: 100
              onPaint: {
                  print("onPaint", "visible:", visible, "height:", height)
                  var ctx = getContext("2d");
                  // Use random color to make it easier to see when onPaint() was called
                  ctx.fillStyle = Qt.rgba(Math.random(),Math.random(),Math.random(), 1);
                  ctx.fillRect(10, 10, width - 20, height - 20);
      
                  ctx.strokeStyle = "pink"
                  ctx.lineWidth = 4
                  ctx.beginPath()
                  ctx.moveTo(10, 10)
                  ctx.lineTo(width - 20, 50)
                  ctx.closePath()
                  ctx.stroke()
              }
          }
      
          Row {
              anchors.bottom: parent.bottom
              spacing: 10
      
              Button {
                  text: "Reset size"
                  onClicked: {
                      // Reset the situation to test again.
                      mycanvas.height = 100;
                  }
              }
      
              Button {
                  text: "Size change while not visible"
                  onClicked: {
                      // This sequence does not result in mycanvas.onPaint().
                      // As we still change the Canvas height, the result look stretched.
                      mycanvas.visible = false;
                      mycanvas.height = 200;
                      mycanvas.visible = true;
                  }
              }
          }
      }
      

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            qt.team.quick.subscriptions Qt Quick and Widgets Team
            lasse.lopperi Lasse Lopperi
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:

              Gerrit Reviews

                There are no open Gerrit changes