我有一个以下的 QML 应用程序:
import QtQuick 2.1
import QtQuick.Controls 1.0
import QtQuick.Layouts 1.0
import QtQuick.Window 2.0
ApplicationWindow {
id: window
width: 480
height: 240
RowLayout {
Rectangle {
width: window.height
height: window.height
radius: window.height / 2
color: "black"
}
Canvas {
id: canvas
width: window.height
height: window.height
onPaint: {
var ctx = canvas.getContext('2d');
var originX = window.height / 2
var originY = window.height / 2
var radius = window.height / 2
ctx.save();
ctx.beginPath();
ctx.arc(originX, originY, radius, 0, 2 * Math.PI);
ctx.fillStyle = Qt.rgba(0, 0, 0, 1);
ctx.fill();
ctx.restore();
}
}
}
}
这会产生两个相邻的黑色圆圈。左边的一个(
矩形
)在Retina显示器上非常清晰,而右边的一个(画布
)则相当模糊。如果我添加 。 antialiasing: false
在Canvas
中,它产生了粗糙模糊的像素。
我需要做什么才能使Canvas
具有HiDPI感知能力?
(我正在Mac OS X 10.8上使用Qt 5.2.0 beta 1)
编辑:我想到的解决方法是将Canvas
包装在一个Item
中,在onPaint
内部放大所有内容,然后在Canvas
上使用一个transform
将其缩小回来。
Canvas {
id: canvas
x: 0
y: 0
width: parent.width * 2 // really parent.width after the transform
heigth: parent.height * 2 // really parent.height after the transform
/* ... */
// This scales everything down by a factor of two.
transform: Scale {
xScale: .5
yScale: .5
}
onPaint: {
var ctx = canvas.getContext('2d');
ctx.save();
ctx.scale(2, 2) // This scales everything up by a factor of two.
/* ... */
}
}