Draw a line between two rectangles by mouse click
-
Hello everyone,
I am new to QML. I have some rectangles in the window. When the user click on one rectangle, I want to draw a line from that rectangle to the point that the mouse is released.
For this, I was trying to define a myRec class with a mouseArea and then using Shape inside the mouse area, as following:
import QtQuick 2.15 Item { property color boxColor:"Orange" property string name: "Box" id: moduleBox x : 400 y: 100 width: 200 height: 100 Rectangle{ color: boxColor anchors.fill: parent radius: 10 Text { color: "black" text: name anchors.centerIn: parent } MouseArea{ anchors.fill: parent hoverEnabled: true cursorShape: Qt.OpenHandCursor onPressed: { Shape{ // an error is generated for this line ShapePath { strokeColor: "black"; startX: parent.x + parent.width/2 startY: parent.y + parent.height PathLine { x: mouseX; y:mouseY } } } } } }
But, it generates the error
Error compiling qml file: .. <PATH-TO-THE-LINE> : error: error: Expected token `,'
Does anyone have an idea about it? It seams that an item can not be used inside the signal handler, did I understand correctly? if yes, how could I solve the problem?
Thank you,
-
Hello everyone,
I am new to QML. I have some rectangles in the window. When the user click on one rectangle, I want to draw a line from that rectangle to the point that the mouse is released.
For this, I was trying to define a myRec class with a mouseArea and then using Shape inside the mouse area, as following:
import QtQuick 2.15 Item { property color boxColor:"Orange" property string name: "Box" id: moduleBox x : 400 y: 100 width: 200 height: 100 Rectangle{ color: boxColor anchors.fill: parent radius: 10 Text { color: "black" text: name anchors.centerIn: parent } MouseArea{ anchors.fill: parent hoverEnabled: true cursorShape: Qt.OpenHandCursor onPressed: { Shape{ // an error is generated for this line ShapePath { strokeColor: "black"; startX: parent.x + parent.width/2 startY: parent.y + parent.height PathLine { x: mouseX; y:mouseY } } } } } }
But, it generates the error
Error compiling qml file: .. <PATH-TO-THE-LINE> : error: error: Expected token `,'
Does anyone have an idea about it? It seams that an item can not be used inside the signal handler, did I understand correctly? if yes, how could I solve the problem?
Thank you,
@vinikiti - Looks like one too many parenthesis after
onPressed:
MouseArea { anchors.fill: parent hoverEnabled: true cursorShape: Qt.OpenHandCursor onPressed: Shape { ShapePath { strokeColor: "black"; startX: parent.x + parent.width/2 startY: parent.y + parent.height PathLine { x: mouseX; y:mouseY } } } }
I cannot fully test your code as I am using a version of QT SDK (SailfishOS) that does not have access (not licensed) to various QML types like Shape, ShapePath, etc