2

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

在此处输入图像描述

我试图设置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"
    }
}

我使用的图标来自谷歌材料设计图标,它是为高 dpi 屏幕设备制作的(分辨率为 192x192)。启用高 dpi 工具按钮中的图标会显得平滑。icon.height如果我禁用高 dpi 支持并将图标 (和icon.width)的高度和宽度设置为640/160 * 48(640是我设备的 dpi ),则图标清晰明了。但是,如果我启用高 dpi 并将高度和宽度设置为48,则图标不清晰。

4

1 回答 1

0

I guess Qt's icon property scales images to increase performance (E.g. the ToolButton is 48 x 48 the source image will be scaled to 48 x 48), but doesn't compensate for HighDpiScaling.

For example your ToolButton is scaled from 48 x 48 logical to 192 x 192 physical pixels by HighDpiScaling, but it is still using the downscaled 48 x 48 image.

I tend to use:

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"
    }
}
于 2019-04-18T15:05:11.110 回答