Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Draw a line between two rectangles by mouse click
Forum Updated to NodeBB v4.3 + New Features

Draw a line between two rectangles by mouse click

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
2 Posts 2 Posters 345 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    vinikiti
    wrote on last edited by
    #1

    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,

    MarkkyboyM 1 Reply Last reply
    0
    • V vinikiti

      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,

      MarkkyboyM Offline
      MarkkyboyM Offline
      Markkyboy
      wrote on last edited by
      #2

      @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

      Don't just sit there standing around, pick up a shovel and sweep up!

      I live by the sea, not in it.

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved