如何使用QtQuick平铺画布?

时间:2015-06-20 08:04:43

标签: qt canvas qml qt5.4

我找到了一份文件here,但要理解它太模糊了。谁能给我一些使用平铺Canvas

的例子

更新
现在,平铺Canvas已标记为已废弃。我们应该 NOT 再使用它。

1 个答案:

答案 0 :(得分:1)

感谢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()
    }
}