import QtQuick 2.4 import QtQuick.Controls 2.5 import QtQuick.Window 2.3 ApplicationWindow { visible: true width: 800 height: 600 title: "Drawing Board Example" property var rectStartPos: null property var rectEndPos: null Item { width: 700 height: 500 property int gridSize: 20 property int scaleFactor: 100 Canvas { id: canvas anchors.fill: parent onPaint: { var ctx = getContext("2d"); var width = canvas.width; var height = canvas.height; // 清除画布 ctx.clearRect(0, 0, width, height); ctx.lineWidth = 0.002 * parent.scaleFactor; // 绘制网格线 ctx.strokeStyle = "black"; for (var x = 0; x <= width; x += parent.gridSize) { ctx.beginPath(); ctx.moveTo(x, 0); ctx.lineTo(x, height); ctx.stroke(); } for (var y = 0; y <= height; y += parent.gridSize) { ctx.beginPath(); ctx.moveTo(0, y); ctx.lineTo(width, y); ctx.stroke(); } function drawRect(x, y, width, height) { var ctx = canvas.getContext("2d"); ctx.strokeStyle = "red"; ctx.strokeRect(x, y, width, height); } // 绘制矩形 if (rectStartPos !== null && rectEndPos !== null) { var x = Math.min(rectStartPos.x, rectEndPos.x); var y = Math.min(rectStartPos.y, rectEndPos.y); var width = Math.abs(rectStartPos.x - rectEndPos.x); var height = Math.abs(rectStartPos.y - rectEndPos.y); drawRect(x, y, width, height); } } } MouseArea { anchors.fill: parent onWheel: { parent.scaleFactor += wheel.angleDelta.y / 120; parent.scaleFactor = Math.max(parent.scaleFactor, 10); parent.gridSize = parent.scaleFactor / 5; canvas.width = width * parent.scaleFactor / 100; canvas.height = height * parent.scaleFactor / 100; canvas.requestPaint(); } onPressed: { rectStartPos = mapToItem(canvas, mouse.x, mouse.y); } onReleased: { rectStartPos = null; rectEndPos = null; } onPositionChanged: { if (rectStartPos !== null) { rectEndPos = mapToItem(canvas, mouse.x, mouse.y); canvas.requestPaint(); } } } Button { id: rectButton text: "Draw Rectangle" anchors.bottom: parent.bottom anchors.horizontalCenter: parent.horizontalCenter onClicked: { console.log("Button clicked"); } } } }这段代码所画出的矩形不能够一直保存,而是在第二次绘画的开始就会将上一次绘画的结果清除,如何保证上一次的绘画结果不被清除,请使用基于QT 的qml语言实现
时间: 2024-03-17 09:45:14 浏览: 69
要保留上一次的绘画结果,可以使用一个数组来保存所绘制的所有矩形的位置和大小,然后在 onPaint 事件中遍历数组,绘制所有的矩形。
下面是修改后的代码:
```qml
import QtQuick 2.4
import QtQuick.Controls 2.5
import QtQuick.Window 2.3
ApplicationWindow {
visible: true
width: 800
height: 600
title: "Drawing Board Example"
property var rectStartPos: null
property var rectEndPos: null
property var rects: []
Item {
width: 700
height: 500
property int gridSize: 20
property int scaleFactor: 100
Canvas {
id: canvas
anchors.fill: parent
onPaint: {
var ctx = getContext("2d");
var width = canvas.width;
var height = canvas.height;
// 清除画布
ctx.clearRect(0, 0, width, height);
// 绘制网格线
ctx.lineWidth = 0.002 * parent.scaleFactor;
ctx.strokeStyle = "black";
for (var x = 0; x <= width; x += parent.gridSize) {
ctx.beginPath();
ctx.moveTo(x, 0);
ctx.lineTo(x, height);
ctx.stroke();
}
for (var y = 0; y <= height; y += parent.gridSize) {
ctx.beginPath();
ctx.moveTo(0, y);
ctx.lineTo(width, y);
ctx.stroke();
}
// 绘制保存的矩形
function drawRect(x, y, width, height) {
var ctx = canvas.getContext("2d");
ctx.strokeStyle = "red";
ctx.strokeRect(x, y, width, height);
}
for (var i = 0; i < parent.rects.length; i++) {
var rect = parent.rects[i];
drawRect(rect.x, rect.y, rect.width, rect.height);
}
// 绘制正在绘制的矩形
if (rectStartPos !== null && rectEndPos !== null) {
var x = Math.min(rectStartPos.x, rectEndPos.x);
var y = Math.min(rectStartPos.y, rectEndPos.y);
var width = Math.abs(rectStartPos.x - rectEndPos.x);
var height = Math.abs(rectStartPos.y - rectEndPos.y);
drawRect(x, y, width, height);
}
}
}
MouseArea {
anchors.fill: parent
onWheel: {
parent.scaleFactor += wheel.angleDelta.y / 120;
parent.scaleFactor = Math.max(parent.scaleFactor, 10);
parent.gridSize = parent.scaleFactor / 5;
canvas.width = width * parent.scaleFactor / 100;
canvas.height = height * parent.scaleFactor / 100;
canvas.requestPaint();
}
onPressed: {
rectStartPos = mapToItem(canvas, mouse.x, mouse.y);
}
onReleased: {
if (rectStartPos !== null && rectEndPos !== null) {
var x = Math.min(rectStartPos.x, rectEndPos.x);
var y = Math.min(rectStartPos.y, rectEndPos.y);
var width = Math.abs(rectStartPos.x - rectEndPos.x);
var height = Math.abs(rectStartPos.y - rectEndPos.y);
parent.rects.push({x: x, y: y, width: width, height: height});
canvas.requestPaint();
}
rectStartPos = null;
rectEndPos = null;
}
onPositionChanged: {
if (rectStartPos !== null) {
rectEndPos = mapToItem(canvas, mouse.x, mouse.y);
canvas.requestPaint();
}
}
}
Button {
id: rectButton
text: "Draw Rectangle"
anchors.bottom: parent.bottom
anchors.horizontalCenter: parent.horizontalCenter
onClicked: {
console.log("Button clicked");
}
}
}
}
```
在代码中,我们新增了一个 rects 数组来保存所绘制的所有矩形的位置和大小。在 onReleased 事件中,当绘制完成一个新的矩形时,我们将其位置和大小存储到 rects 数组中,并调用 canvas.requestPaint() 方法来触发重新绘制。在 onPaint 事件中,我们遍历 rects 数组,绘制所有保存的矩形。
阅读全文