QML 缩放不适用于非整数缩放因子 [英] QML scaling does not work with non-integer scaling factors

查看:23
本文介绍了QML 缩放不适用于非整数缩放因子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 QML 编写应用程序.当我按非整数因子缩放 GUI 时,我遇到了问题.根据文档Qt::AA_EnableHighDpiScaling应该启用独立于设备的像素,因此会自动处理大部分缩放:

I'm writing an application using QML. I'm having trouble when I scale my GUI by non-integer factors. According to the docs, Qt::AA_EnableHighDpiScaling should enable device-independent pixels, therefore automatically taking care of most of the scaling:

应用属性Qt::AA_EnableHighDpiScaling,在Qt 5.6,启用基于像素密度的自动缩放监控.

The application attribute Qt::AA_EnableHighDpiScaling, introduced in Qt 5.6, enables automatic scaling based on the pixel density of the monitor.

关于 5.6 的博文,他们承认可能存在问题:

In a blog post about 5.6, they admit that there can be problems:

A:Qt 在API,并允许通过设置非整数比例因子QT_SCALE_FACTOR.但是,Qt 不保证图形和在这种情况下,样式将是无故障的.样式可能会先崩溃:融合风格通常是最具可扩展性的.Qt 平台插件回合报告的比例因子到最接近的整数.

Q: Are non-integer scale factors supported?

A: Qt uses qreal in the API, and will allow setting non-integer scale factors via QT_SCALE_FACTOR. However, Qt does not guarantee that graphics and styles will be glitch-free in that case. Styles may break first: the fusion style is generally most scalable. The Qt platform plugins round the reported scale factors to the nearest integer.

并在评论中:

问:这是否意味着它仍然是有效的整数?150% DPI 比例的 Windows 会发生什么?

Q: Does that mean it’s still effectively integer-only? What happens to Windows with 150% DPI scale?

A:是的,除非您使用 QT_SCALE_FACTOR 手动设置/更正它.然后 150% 应该变成 2 倍.

A: Yes, unless you set/correct it manually with QT_SCALE_FACTOR. 150% should then go to 2x.

所以对我来说,当缩放到 150% 时,这会导致一个可笑的大 GUI.但是,文本缩放正确,这会导致奇怪的伪像,例如带有小文本的大按钮.

So for me this leads to a comically large GUI when scaling to 150%. However, the text scales correctly which leads to weird artifacts such as large button with small text.

我是否误解了这是如何工作的,还是真的不可能?

Am I misunderstanding how this works or is it just not really possible yet?

推荐答案

正如评论中提到的,我对 QT_SCALE_FACTOR 不满意,所以我决定自己做,并创建了一个 ScaleableWindow 像这样:

As mentioned in the comment, I was not satisfied by QT_SCALE_FACTOR so I decided to make it my self, and created a ScaleableWindow like this:

import QtQuick 2.0
import QtQuick.Controls 2.0

ApplicationWindow {
    id: root
    property real scale: 1
    property real unscaledWidth: 100
    property real unscaledHeight: 100

    Component.onCompleted: {
        var i = Qt.application.arguments.indexOf('--scale')
        if (i > -1 && Qt.application.arguments[i+1]) scale = parseFloat(Qt.application.arguments[i+1])
    }

    width: unscaledWidth * scale
    height: unscaledHeight * scale

    property alias scaledContentItem: scaledContent
    default property alias scaledContent: scaledContent.data

    Item {
        id: scaledContent
        width: root.unscaledWidth
        height: root.unscaledHeight
        scale: root.scale
        anchors.centerIn: parent
    }
}

您现在可以通过传递例如指定比例因子命令行参数 --scale 0.4

You can now specify the scale-factor by passing e.g. the commandline argument --scale 0.4

您也可以尝试使用 Screen.pixelDensity 来计算缩放因子,但这依赖于显示器正确发布其像素密度,这对我来说经常失败.

You can also try to use Screen.pixelDensity to calculate a scaleing factor, but that relies on the display to correctly publish its pixel density, which failed for me quite often.

您还可以使用它来创建一个窗口,当您调整窗口大小时,该窗口会自动缩放内容.

You can also use this to create a window that scales the content automatically, when you resize the window.

所以如果问题是环境变量是否有可能 - 我不这么认为.如果问题是是否可以根据一些外部输入缩放窗口及其内容 - 这里有一个解决方案.

So if the question is Is it possible with the environment variable - I don't think so. If the question is Is it possible to scale the window and its content based on some external input - here is a solution.

这篇关于QML 缩放不适用于非整数缩放因子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆