如何使用QtQuick平铺canvas?
How to use QtQuick tiled canvas?
我找到了一份文件 here,但它太含糊不清了。谁能给我一些使用平铺的例子 Canvas
?
更新
现在平铺 Canvas
被标记为已废弃。我们应该不要再使用它。
感谢 Mitch 的评论,我们知道平铺 Canvas
并不总是正常工作(参见 here)。
该错误仍然出现在 Qt 5.4.2 中,如下例所示:
import QtQuick 2.0
Rectangle {
width: 400
height: 400
Flickable {
id: flick
anchors.fill: parent
contentWidth: 400
contentHeight: 40000
}
Canvas {
parent: flick
anchors.fill: parent
contextType: "2d"
canvasSize: Qt.size(flick.contentWidth, flick.contentHeight)
canvasWindow: Qt.rect(flick.contentX, flick.contentY,
flick.width, flick.height)
tileSize: Qt.size(20, 20)
renderStrategy: Canvas.Cooperative
onPaint: {
for (var y = region.y; y < region.y + region.height;
y += tileSize.height) {
for (var x = region.x; x < region.x + region.width;
x += tileSize.width) {
context.fillStyle = "black";
context.fillRect(x, y, tileSize.width, tileSize.height);
context.fillStyle = "green";
context.fillRect(x + 1, y + 1, tileSize.width - 2,
tileSize.height - 2);
}
}
}
onCanvasWindowChanged: requestPaint()
}
}
我找到了一份文件 here,但它太含糊不清了。谁能给我一些使用平铺的例子 Canvas
?
更新
现在平铺 Canvas
被标记为已废弃。我们应该不要再使用它。
感谢 Mitch 的评论,我们知道平铺 Canvas
并不总是正常工作(参见 here)。
该错误仍然出现在 Qt 5.4.2 中,如下例所示:
import QtQuick 2.0
Rectangle {
width: 400
height: 400
Flickable {
id: flick
anchors.fill: parent
contentWidth: 400
contentHeight: 40000
}
Canvas {
parent: flick
anchors.fill: parent
contextType: "2d"
canvasSize: Qt.size(flick.contentWidth, flick.contentHeight)
canvasWindow: Qt.rect(flick.contentX, flick.contentY,
flick.width, flick.height)
tileSize: Qt.size(20, 20)
renderStrategy: Canvas.Cooperative
onPaint: {
for (var y = region.y; y < region.y + region.height;
y += tileSize.height) {
for (var x = region.x; x < region.x + region.width;
x += tileSize.width) {
context.fillStyle = "black";
context.fillRect(x, y, tileSize.width, tileSize.height);
context.fillStyle = "green";
context.fillRect(x + 1, y + 1, tileSize.width - 2,
tileSize.height - 2);
}
}
}
onCanvasWindowChanged: requestPaint()
}
}