import QtQml 2.14
import QtQuick 2.15
Rectangle{
id:root
color:"red"
property bool isSelFirstRect: true
Image {
id: background
anchors{
fill:parent
}
source: "qrc:/image/background.jpeg"
}
onIsSelFirstRectChanged: {
canvas.requestPaint()
}
Rectangle{
id:firstRect
x:100
y:100
width: 200
height: 200
color: "transparent"
border.width: 2
border.color: root.isSelFirstRect?"green":"#FFFFFF"
onWidthChanged: {
canvas.requestPaint()
}
onHeightChanged: {
canvas.requestPaint()
}
onXChanged: {
canvas.requestPaint()
}
onYChanged: {
canvas.requestPaint()
}
//左上
Rectangle{
visible: root.isSelFirstRect
width: 20
height: 6
anchors{
left: parent.left
top: parent.top
}
color: "green"
}
Rectangle{
visible: root.isSelFirstRect
width: 6
height: 20
anchors{
left: parent.left
top: parent.top
}
color: "green"
}
Rectangle{
visible: root.isSelFirstRect
width: 6
height: 6
anchors{
left: parent.left
top: parent.top
}
color: "green"
MouseArea{
anchors.fill: parent
hoverEnabled: true
cursorShape:Qt.SizeFDiagCursor
property var clickPt: Qt.point(0,0)
property bool isPressed: false
onPressed: {
clickPt=Qt.point(mouseX,mouseY)
isPressed=true
}
onMouseXChanged: {
var dealt=Qt.point(mouseX-clickPt.x,mouseY-clickPt.y)
if(isPressed&&firstRect.width-dealt.x>40&&firstRect.height-dealt.y>40){
// clickPt=Qt.point(mouseX,mouseY)
firstRect.x=firstRect.x+dealt.x
firstRect.y=firstRect.y+dealt.y
firstRect.width-=dealt.x
firstRect.height-=dealt.y
}
}
onReleased: {
isPressed=false
}
}
z:3
}
//右下
Rectangle{
visible: root.isSelFirstRect
width: 20
height: 6
anchors{
right: parent.right
bottom: parent.bottom
}
color: "green"
}
Rectangle{
visible: root.isSelFirstRect
width: 6
height: 20
anchors{
right: parent.right
bottom: parent.bottom
}
color: "green"
}
Rectangle{
visible: root.isSelFirstRect
width: 6
height: 6
anchors{
right: parent.right
bottom: parent.bottom
}
color: "green"
MouseArea{
anchors.fill: parent
hoverEnabled: true
cursorShape:Qt.SizeFDiagCursor
property var clickPt: Qt.point(0,0)
property bool isPressed: false
onPressed: {
clickPt=Qt.point(mouseX,mouseY)
isPressed=true
}
onMouseXChanged: {
var dealt=Qt.point(mouseX-clickPt.x,mouseY-clickPt.y)
if(isPressed&&firstRect.width+dealt.x>40&&firstRect.height+dealt.y>40){
firstRect.width+=dealt.x
firstRect.height+=dealt.y
}
}
onReleased: {
isPressed=false
}
}
z:3
}
//右上
Rectangle{
visible: root.isSelFirstRect
width: 20
height: 6
anchors{
right: parent.right
top: parent.top
}
color: "green"
}
Rectangle{
visible: root.isSelFirstRect
width: 6
height: 20
anchors{
right: parent.right
top: parent.top
}
color: "green"
}
Rectangle{
visible: root.isSelFirstRect
width: 6
height: 6
anchors{
right: parent.right
top: parent.top
}
color: "green"
MouseArea{
anchors.fill: parent
hoverEnabled: true
cursorShape:Qt.SizeBDiagCursor
property var clickPt: Qt.point(0,0)
property bool isPressed: false
onPressed: {
clickPt=Qt.point(mouseX,mouseY)
isPressed=true
}
onPositionChanged: {
var dealt=Qt.point(mouseX-clickPt.x,mouseY-clickPt.y)
if(isPressed&&firstRect.width+dealt.x>40&&firstRect.height-dealt.y>40){
// clickPt=Qt.point(mouseX,mouseY)
firstRect.y=firstRect.y+dealt.y
firstRect.width+=dealt.x
firstRect.height-=dealt.y
}
}
onReleased: {
isPressed=false
}
}
z:3
}
//左下
Rectangle{
visible: root.isSelFirstRect
width: 20
height: 6
anchors{
left: parent.left
bottom: parent.bottom
}
color: "green"
}
Rectangle{
visible: root.isSelFirstRect
width: 6
height: 20
anchors{
left: parent.left
bottom: parent.bottom
}
color: "green"
}
Rectangle{
visible: root.isSelFirstRect
width: 6
height: 6
anchors{
left: parent.left
bottom: parent.bottom
}
color: "green"
MouseArea{
anchors.fill: parent
enabled: true
hoverEnabled: true
cursorShape:Qt.SizeBDiagCursor
property var clickPt: Qt.point(0,0)
property bool isPressed: false
onPressed: {
clickPt=Qt.point(mouseX,mouseY)
isPressed=true
}
onMouseXChanged: {
var dealt=Qt.point(mouseX-clickPt.x,mouseY-clickPt.y)
if(isPressed&&firstRect.width-dealt.x>40&&firstRect.height+dealt.y>40){
firstRect.x=firstRect.x+dealt.x
firstRect.width-=dealt.x
firstRect.height+=dealt.y
}
}
onReleased: {
isPressed=false
}
}
z:3
}
Rectangle{
visible: root.isSelFirstRect
width: 20
height: 2
color: "green"
anchors.centerIn: parent
}
Rectangle{
visible: root.isSelFirstRect
width: 2
height: 20
color: "green"
anchors.centerIn: parent
}
MouseArea{
anchors.fill: parent
// hoverEnabled: true
onPressed: {
if(mouse.button==Qt.LeftButton){
root.isSelFirstRect=true
}
}
drag.target: firstRect
drag.axis: Drag.XAndYAxis
}
}
Rectangle{
id:secondRect
x:500
y:500
width: 200
height: 200
color: "transparent"
border.width: 2
border.color: !root.isSelFirstRect?"green":"#FFFFFF"
onWidthChanged: {
canvas.requestPaint()
}
onHeightChanged: {
canvas.requestPaint()
}
onXChanged: {
canvas.requestPaint()
}
onYChanged: {
canvas.requestPaint()
}
//左上
Rectangle{
visible: !root.isSelFirstRect
width: 20
height: 6
anchors{
left: parent.left
top: parent.top
}
color: "green"
}
Rectangle{
visible: !root.isSelFirstRect
width: 6
height: 20
anchors{
left: parent.left
top: parent.top
}
color: "green"
}
Rectangle{
visible: !root.isSelFirstRect
width: 6
height: 6
anchors{
left: parent.left
top: parent.top
}
color: "green"
MouseArea{
anchors.fill: parent
hoverEnabled: true
cursorShape:Qt.SizeFDiagCursor
property var clickPt: Qt.point(0,0)
property bool isPressed: false
onPressed: {
clickPt=Qt.point(mouseX,mouseY)
isPressed=true
}
onMouseXChanged: {
var dealt=Qt.point(mouseX-clickPt.x,mouseY-clickPt.y)
if(isPressed&&secondRect.width-dealt.x>40&&secondRect.height-dealt.y>40){
secondRect.x=secondRect.x+dealt.x
secondRect.y=secondRect.y+dealt.y
secondRect.width-=dealt.x
secondRect.height-=dealt.y
}
}
onReleased: {
isPressed=false
}
}
z:3
}
//右下
Rectangle{
visible: !root.isSelFirstRect
width: 20
height: 6
anchors{
right: parent.right
bottom: parent.bottom
}
color: "green"
}
Rectangle{
visible: !root.isSelFirstRect
width: 6
height: 20
anchors{
right: parent.right
bottom: parent.bottom
}
color: "green"
}
Rectangle{
visible: !root.isSelFirstRect
width: 6
height: 6
anchors{
right: parent.right
bottom: parent.bottom
}
color: "green"
MouseArea{
anchors.fill: parent
hoverEnabled: true
cursorShape:Qt.SizeFDiagCursor
property var clickPt: Qt.point(0,0)
property bool isPressed: false
onPressed: {
clickPt=Qt.point(mouseX,mouseY)
isPressed=true
}
onMouseXChanged: {
var dealt=Qt.point(mouseX-clickPt.x,mouseY-clickPt.y)
if(isPressed&&secondRect.width+dealt.x>40&&secondRect.height+dealt.y>40){
// clickPt=Qt.point(mouseX,mouseY)
secondRect.width+=dealt.x
secondRect.height+=dealt.y
}
}
onReleased: {
isPressed=false
}
}
z:3
}
//右上
Rectangle{
visible: !root.isSelFirstRect
width: 20
height: 6
anchors{
right: parent.right
top: parent.top
}
color: "green"
}
Rectangle{
visible: !root.isSelFirstRect
width: 6
height: 20
anchors{
right: parent.right
top: parent.top
}
color: "green"
}
Rectangle{
visible: !root.isSelFirstRect
width: 6
height: 6
anchors{
right: parent.right
top: parent.top
}
color: "green"
MouseArea{
anchors.fill: parent
hoverEnabled: true
cursorShape:Qt.SizeBDiagCursor
property var clickPt: Qt.point(0,0)
property bool isPressed: false
onPressed: {
clickPt=Qt.point(mouseX,mouseY)
isPressed=true
}
onMouseXChanged: {
var dealt=Qt.point(mouseX-clickPt.x,mouseY-clickPt.y)
if(isPressed&&secondRect.width+dealt.x>40&&secondRect.height-dealt.y>40){
// clickPt=Qt.point(mouseX,mouseY)
secondRect.y=secondRect.y+dealt.y
secondRect.width+=dealt.x
secondRect.height-=dealt.y
}
}
onReleased: {
isPressed=false
}
}
z:3
}
//左下
Rectangle{
visible: !root.isSelFirstRect
width: 20
height: 6
anchors{
left: parent.left
bottom: parent.bottom
}
color: "green"
}
Rectangle{
visible: !root.isSelFirstRect
width: 6
height: 20
anchors{
left: parent.left
bottom: parent.bottom
}
color: "green"
}
Rectangle{
visible: !root.isSelFirstRect
width: 6
height: 6
anchors{
left: parent.left
bottom: parent.bottom
}
color: "green"
MouseArea{
anchors.fill: parent
enabled: true
hoverEnabled: true
cursorShape:Qt.SizeBDiagCursor
property var clickPt: Qt.point(0,0)
property bool isPressed: false
onPressed: {
clickPt=Qt.point(mouseX,mouseY)
isPressed=true
}
onMouseXChanged: {
var dealt=Qt.point(mouseX-clickPt.x,mouseY-clickPt.y)
if(isPressed&&secondRect.width-dealt.x>40&&secondRect.height+dealt.y>40){
secondRect.x=secondRect.x+dealt.x
secondRect.width-=dealt.x
secondRect.height+=dealt.y
}
}
onReleased: {
isPressed=false
}
}
z:3
}
Rectangle{
visible: !root.isSelFirstRect
width: 20
height: 2
color: "green"
anchors.centerIn: parent
}
Rectangle{
visible: !root.isSelFirstRect
width: 2
height: 20
color: "green"
anchors.centerIn: parent
}
MouseArea{
anchors.fill: parent
onPressed: {
root.isSelFirstRect=false
}
drag.target: secondRect
drag.axis: Drag.XAndYAxis
}
}
Rectangle {
id:topRect
anchors{
left: parent.left
right: parent.right
top: parent.top
bottom: root.isSelFirstRect?firstRect.top:secondRect.top
}
z:2
opacity: 0.5
color: "black"
}
Rectangle {
id:bottomRect
anchors{
left: parent.left
right: parent.right
bottom: parent.bottom
top: root.isSelFirstRect?firstRect.bottom:secondRect.bottom
}
z:2
opacity: 0.5
color: "black"
}
Rectangle {
id:leftRect
anchors{
left: parent.left
right: root.isSelFirstRect?firstRect.left:secondRect.left
bottom: bottomRect.top
top: topRect.bottom
}
z:2
opacity: 0.5
color: "black"
}
Rectangle {
id:rightRect
anchors{
left: root.isSelFirstRect?firstRect.right:secondRect.right
right:parent.right
bottom: bottomRect.top
top: topRect.bottom
}
z:2
opacity: 0.5
color: "black"
}
Canvas{
id:canvas
contextType: "2d"
antialiasing: true
anchors{
fill:parent
}
function arrow(context, fromx, fromy, tox, toy) {
const dx = tox - fromx;
const dy = toy - fromy;
const headlen = Math.sqrt(dx * dx + dy * dy) * 0.05; // length of head in pixels
const angle = Math.atan2(dy, dx);
context.beginPath();
context.moveTo(fromx,fromy)
context.lineTo(tox - headlen * Math.cos(angle - Math.PI / 6)*0.5, toy - headlen * Math.sin(angle - Math.PI / 6)*0.5)
context.lineTo(tox,toy)
context.lineTo(tox - headlen * Math.cos(angle + Math.PI / 6)*0.5, toy - headlen * Math.sin(angle + Math.PI / 6)*0.5)
context.stroke();
context.fill()
context.beginPath();
context.moveTo(tox - headlen * Math.cos(angle - Math.PI / 6), toy - headlen * Math.sin(angle - Math.PI / 6));
context.lineTo(tox, toy );
context.lineTo(tox - headlen * Math.cos(angle + Math.PI / 6), toy - headlen * Math.sin(angle + Math.PI / 6));
context.stroke();
context.fill()
}
onPaint: {
var context=getContext("2d")
context.clearRect(0,0,width,height)
context.lineWidth = 1
context.strokeStyle = "#75FA61"
context.fillStyle = "#75FA61"
var fromPt=root.isSelFirstRect?Qt.point(firstRect.x+firstRect.width/2,firstRect.y+firstRect.height/2):Qt.point(secondRect.x+secondRect.width/2,secondRect.y+secondRect.height/2)
var toPt=root.isSelFirstRect?Qt.point(secondRect.x+secondRect.width/2,secondRect.y+secondRect.height/2):Qt.point(firstRect.x+firstRect.width/2,firstRect.y+firstRect.height/2)
arrow(context,toPt.x,toPt.y,fromPt.x,fromPt.y)
}
z: 2
}
}
相关文档参考文档
Canvas学习:绘制箭头
在这里插入图片描述