【问题标题】:What is necessary to make QtQuick 2's Canvas element HiDPI-(retina-)aware?使 QtQuick 2 的 Canvas 元素 HiDPI-(retina-) 感知需要什么?
【发布时间】:2013-10-24 18:13:19
【问题描述】:

我有以下 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();
            }

        }
    }
}

这会产生两个相邻的黑色圆圈。左侧(Rectangle)在 Retina 显示屏上清晰,而右侧(Canvas)则相当模糊。如果我添加

                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.

            /* ... */
        }
    }

【问题讨论】:

    标签: canvas qml retina-display


    【解决方案1】:

    对于ProgressCircle in qml-material,我们使用了相同的技巧,将大小翻倍然后缩小。但是,您可以进行一些改进:

    1. 使用scale 而不是transform
    2. 使用 QtQuick.Window 模块中的 Screen.devicePixelRatio,而不是将比例因子硬编码为 2/0.5。

    所以你的代码可以简化为:

    Canvas {
        property int ratio: Screen.devicePixelRatio
    
        width: parent.width * ratio
        heigth: parent.height * ratio
        scale: 1/ratio
    
        onPaint: {
            var ctx = canvas.getContext('2d');
            ctx.save();
            ctx.scale(ratio, ratio)
    
            // ...
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2018-05-07
      • 2018-03-01
      • 2012-07-21
      • 1970-01-01
      • 1970-01-01
      • 2013-01-16
      • 1970-01-01
      • 2021-05-10
      • 1970-01-01
      相关资源
      最近更新 更多