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" Item { width: 400 height: 400 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.strokeStyle = "black"; ctx.lineWidth = 0.2; // 将线宽改小 for (var x = -width/2; x <= width/2; x += parent.gridSize) { ctx.beginPath(); ctx.moveTo(x, -height/2); ctx.lineTo(x, height/2); ctx.stroke(); } for (var y = -height/2; y <= height/2; y += parent.gridSize) { ctx.beginPath(); ctx.moveTo(-width/2, y); ctx.lineTo(width/2, y); ctx.stroke(); } } } MouseArea { anchors.fill: parent property int gridSize: parent.gridSize property int scaleFactor: parent.scaleFactor onWheel: { // 根据滚轮事件的delta属性,计算缩放比例 parent.scaleFactor += wheel.angleDelta.y / 120; parent.scaleFactor = Math.max(parent.scaleFactor, 10); // 最小值为10% // 根据缩放比例重新计算gridSize和canvas的宽度和高度 var newGridSize = parent.scaleFactor / 5; var newWidth = width * parent.scaleFactor / 100; var newHeight = height * parent.scaleFactor / 100; var xDiff = (newWidth - width) / 2; // 计算横向偏移量 var yDiff = (newHeight - height) / 2; // 计算纵向偏移量 parent.gridSize = newGridSize; canvas.width = newWidth; canvas.height = newHeight; canvas.x -= xDiff; // 调整canvas的位置 canvas.y -= yDiff; canvas.requestPaint(); } } } }这段代码的画布大小相对而言变得太小了,请找出问题所在,并改进
时间: 2024-03-17 08:41:11 浏览: 115
Qt Quick App的两种启动方式.docx
问题所在是在缩放比例计算与应用时,没有正确计算新的画布宽高和偏移量,导致画布缩放后被裁剪。正确的代码应该是:
```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"
Item {
width: 400
height: 400
property int gridSize: 20
property real scaleFactor: 1.0
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.strokeStyle = "black";
ctx.lineWidth = 0.2 * parent.scaleFactor;
for (var x = -width/2; x <= width/2; x += parent.gridSize) {
ctx.beginPath();
ctx.moveTo(x, -height/2);
ctx.lineTo(x, height/2);
ctx.stroke();
}
for (var y = -height/2; y <= height/2; y += parent.gridSize) {
ctx.beginPath();
ctx.moveTo(-width/2, y);
ctx.lineTo(width/2, y);
ctx.stroke();
}
}
}
MouseArea {
anchors.fill: parent
property int gridSize: parent.gridSize
property real scaleFactor: parent.scaleFactor
onWheel: {
// 根据滚轮事件的delta属性,计算缩放比例
parent.scaleFactor += wheel.angleDelta.y / 120;
parent.scaleFactor = Math.max(parent.scaleFactor, 0.1); // 最小值为10%
// 根据缩放比例重新计算gridSize和canvas的宽度和高度
var newGridSize = parent.gridSize * parent.scaleFactor;
var newWidth = width * parent.scaleFactor;
var newHeight = height * parent.scaleFactor;
// 计算横向和纵向偏移量
var xDiff = (newWidth - width) / 2;
var yDiff = (newHeight - height) / 2;
parent.gridSize = newGridSize;
canvas.width = newWidth;
canvas.height = newHeight;
canvas.x -= xDiff;
canvas.y -= yDiff;
canvas.requestPaint();
}
}
}
}
```
主要改动是:
- 将scaleFactor从int类型改为real类型,以便支持小数点以下的缩放比例。
- 在绘制网格线时,将线宽乘以缩放比例。
- 在计算新的宽高和偏移量时,将缩放比例乘以原始的gridSize,而不是除以5。
- 在计算偏移量时,将横向和纵向偏移分别计算,避免画布只发生横向或纵向偏移的情况。
阅读全文