0

In the following code a point can be removed from listmodel and in the next line it says ReferenceError : pthModel is not defined. Could you help me please?

import QtQuick
import QtQuick.Shapes 2.15
import "utils.js" as JS

Rectangle {
   id: mRct
   implicitHeight: 600
   implicitWidth: 800
   color: "transparent"

   property bool moving: false
   ListModel {
      id: pthModel

      onDataChanged: {
         console.log("dataChanged")
         cnvManual.requestPaint()
      }
   }

   MouseArea {
      anchors.fill: parent
      id: mouse
      onClicked: {
         if (moving)
            return
         pthModel.append({
                            "x": mouseX,
                            "y": mouseY
                         })
         cnvManual.requestPaint()
      }
   }

   Repeater {
      model: pthModel
      z: 21
      delegate: Rectangle {
         required property var modelData
         id: rctParent
         color: "white"
         width: 12
         height: 12
         radius: 6
         x: modelData.x - 6
         y: modelData.y - 6

         DragHandler {
            acceptedModifiers: Qt.ControlModifier
            target: parent

            onActiveChanged: {
               if (!active) {
                  modelData.x = rctParent.x + 6
                  modelData.y = rctParent.y + 6
                  cnvManual.requestPaint()
               }
            }
         }

         MouseArea {
            anchors.fill: parent
            id: ms
            acceptedButtons: Qt.AllButtons
            signal removed

            onRemoved: {
               console.log("removed")
               cnvManual.requestPaint()
            }
            onClicked: mouse => {
                          if (mouse.button === Qt.RightButton) {
                             //cnvManual.requestPaint() //Works
                             pthModel.remove(modelData.index, 1)
                           //cnvManual.requestPaint() //cnvManual is not defined error
                           //whatever happens it happens after this line
                             ms.removed() //msRemoved is not triggered
                          }
                       }
         }
      }
   }

   Canvas {
      id: cnvManual
      anchors.fill: parent

      onPaint: {
         if (pthModel.count === 0) {
            return
         }
         console.log(pthModel.count)
         var ctx = getContext("2d")
         ctx.clearRect(0, 0, width, height)

         ctx.strokeStyle = "blue"
         ctx.lineWidth = 2

         ctx.beginPath()
         var points = pthModel
         var firstPoint = points.get(0)
         ctx.moveTo(firstPoint.x, firstPoint.y)
         for (var i = 1; i < points.count; i++) {
            var pnt = pthModel.get(i)
            ctx.lineTo(pnt.x, pnt.y)
         }
         if (points.count > 2) {
            ctx.lineTo(firstPoint.x, firstPoint.y)
         }
         ctx.stroke()
      }
   }
}

I tried to trigger pthModel.datachanged event and i get the same reference is not defined error. It does not matter whatever i try, i could not make it to work.

By the way i am open to new ideas to add remove points , drawing lines between them and moving points as well.

Thanks in advance.

2
  • Is the code snippet provided a minimal repro example? If not, please take steps to shorten the code before posting. Can you improve the title of your question? Commented Dec 5, 2023 at 7:13
  • Could it be there is a non-visible character somewhere in that block? Visually it looks correct indeed. As for other ways of doing things, you might want to look into Qml Shapes, particularly doc.qt.io/qt-6/qml-qtquick-pathpolyline.html
    – Amfasis
    Commented Dec 5, 2023 at 7:58

1 Answer 1

0

I have found a solution. I added onItemRemoved to repeater then called Canvas.requestPaint().

 Repeater {
  id: pthRpt
  model: pthModel
  z: 21
  onItemRemoved: cnvManual.requestPaint()
  delegate: Rectangle {
     required property var modelData
     id: rctParent
     color: "white"
     width: 12
     height: 12
     radius: 6

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.