Qt应用程序中的高dpi缩放比例可平滑图标

时间:2018-08-07 01:00:15

标签: android qt qml highdpi

我已经使用QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling);在Qt应用程序中启用了高dpi支持,但是现在我的图像看起来不清晰,而是看起来“平滑”了。例如,下面有一些打算使用高dpi图像的按钮的图片。当我禁用高dpi支持并手动缩放ui时,使用相同的图像,图标清晰明了。

enter image description here

我尝试设置Qt::AA_UseHighDpiPixmaps失败。 这是示例代码:

import QtQuick 2.11
import QtQuick.Window 2.11
import QtQuick.Controls 2.2

Window {
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")

    ToolButton{
        anchors.centerIn: parent
        icon.source: "qrc:/ic_star_white_48dp.png"
    }
}

我使用的图标来自Google物料设计图标,它是为高dpi屏幕设备(分辨率为192x192)制成的。启用高dpi时,工具按钮中的图标将变平滑。如果我禁用了高dpi支持,并将图标(icon.heighticon.width)的高度和宽度设置为640/160 * 48640是设备的dpi),则图标酥脆清晰。但是,如果启用高dpi并将高度和宽度设置为48,则图标不清晰。

1 个答案:

答案 0 :(得分:0)

我猜Qt的icon属性可以缩放图像以提高性能(例如,ToolButton为48 x 48,源图像将缩放为48 x 48),但不能补偿HighDpiScaling。

例如,您的ToolButton通过HighDpiScaling从48 x 48逻辑像素缩放到192 x 192物理像素,但是它仍然使用缩小的48 x 48图像。

我倾向于使用:

ToolButton{
anchors.centerIn: parent

    Image {
        anchors.centerIn: parent
        anchors.margins: 5
        //You can play around with those to balance quality and performance
        //mipmap: true
        //sourceSize.height: height * 2
        //sourceSize.width: width * 2
        fillMode: Image.PreserveAspectFit
        source: "qrc:/image.png"
    }
}