使用画布动态绘制点对点线

时间:2019-03-24 15:08:09

标签: qt qml

我正在尝试制作一个简单的图形应用程序,在其中可以动态创建不同的形状(如(矩形,圆形,三角形))以及一条线(从一个点到另一个)。对于前三个,我还没有遇到任何问题。对于rect,我只定义了一个Rectangle组件,并在我的绘图区域的MouseArea的onClicked Handler中使用create组件创建了它。对于三角形,我与画布一起绘制了三角形,并使用create component和onClicked处理函数创建了该组件。我主要通过mouse.x和mouse.y来在需要的地方创建形状。现在,对于Line,我想定义单击鼠标区域时的起点和释放时的终点。一旦发布,我想画线(使用画布)。我该怎么做呢?

作为参考,我添加了有关如何创建其他项目的屏幕截图: creating the objects

drawing the triangle

1 个答案:

答案 0 :(得分:1)

MouseArea也有一个released信号。每当您的selectedIndex说“ line”时,将x和y存储在onPressed处理程序中,并仅使用存储的位置在onReleased处理程序中创建行

MouseArea {

    property var startPoint
    onPressed: { 
        if(selectedShape.currentIndex === 3) 
            startPoint = Qt.point(mouse.x, mouse.y) 
    }

    onReleased: {
        if(selectedShape.currentIndex === 3 && startPoint !== undefined)
        {
            createLine(startPoint, Qt.point(mouse.x, mouse.y) //your function goes here
        }
        startPoint = undefined        
    }
}

PS。不要将代码粘贴为图片!

相关问题