简体   繁体   English

Qt Quick Controls 2 Scaling

[英]Qt Quick Controls 2 Scaling

I'm developing the application that should run on the mobile and desktop platforms. 我正在开发应该在移动和桌面平台上运行的应用程序。 The problem that I found that sizes of controls is vary on different screens: in high density screens controls is too small and in low density screens is rather big. 我发现控件尺寸在不同屏幕上有所不同的问题:在高密度屏幕中控件太小而在低密度屏幕中则相当大。

I can calculate the scale factor for each screen (ie use Android's Density-independent Pixels) and use it to define item sizes, margins, etc in dp : 我可以计算每个屏幕的比例因子(即使用Android的密度无关像素)并使用它来定义dp项目大小,边距等:

ApplicationWindow {
    ...
    property real dp: Screen.pixelDensity * 10 * 2.54 / 160
    Item {
        width: 50*dp
        height: 50*dp
        ...
        Label {
            font.pixelSize: 16*dp
            ...
        }
    }
}

It work well but it seems that sizes of standard controls in Qt Quick Controls 2 is defined in pixels, so they doesn't scale. 它工作得很好,但似乎Qt Quick Controls 2中标准控件的大小以像素为单位定义,因此它们不会缩放。 The only way that I see is to redefine all controls in Qt Quick Controls 2 using dp instead of pixels. 我看到的唯一方法是使用dp而不是像素重新定义Qt Quick Controls 2中的所有控件。

So I'm looking for method to scale standard controls without redefining them all. 所以我正在寻找扩展标准控件的方法,而无需重新定义它们。

UPD1. UPD1。 I've tried High-DPI Support, it makes situation better but still there is some issue. 我已经尝试过高DPI支持,它使情况更好但仍有一些问题。 Here is some some parameters of primary screen ( see paramter description here ) from different devices before and after applying High-DPI Support: 以下是应用High-DPI支持之前和之后来自不同设备的主屏幕的一些参数( 请参阅此处的参数说明 ):

// samsung tab t-280 without high dpi support
devicePixelRatio 1
geometry QRect(0,0 800x1280)
logicalDotsPerInch 95.85
physicalDotsPerInch 216.458
physicalSize QSizeF(94, 150) (7')

// samsung tab t-280 with high dpi support
devicePixelRatio 1.33125
geometry QRect(0,0 601x962)
logicalDotsPerInch 72
physicalDotsPerInch 162.648
physicalSize QSizeF(94, 150) (7')


// xiaomi redmi 2 without high dpi support
devicePixelRatio 1
geometry QRect(0,0 720x1280)
logicalDotsPerInch 144
physicalDotsPerInch 315.48
physicalSize QSizeF(58, 103) (4.6')

// xiaomi redmi 2 with high dpi support
devicePixelRatio 2
geometry QRect(0,0 360x640)
logicalDotsPerInch 72
physicalDotsPerInch 157.74
physicalSize QSizeF(58, 103) (4.6')


// macbook pro retina 13' without high dpi support
devicePixelRatio 2
geometry QRect(0,0 1280x800)
logicalDotsPerInch 72
physicalDotsPerInch 113.5
physicalSize QSizeF(286.449, 179.031) (13')

// macbook pro retina 13' with high dpi support
devicePixelRatio 2
geometry QRect(0,0 1280x800)
logicalDotsPerInch 72
physicalDotsPerInch 113.5
physicalSize QSizeF(286.449, 179.031) (13')


// generic 20' display without high dpi support
devicePixelRatio 1
geometry QRect(0,0 1280x1024)
logicalDotsPerInch 72
physicalDotsPerInch 72
physicalSize QSizeF(451.556, 361.244) (22.6')

// generic 20' display with high dpi support
devicePixelRatio 1
geometry QRect(0,0 1280x1024)
logicalDotsPerInch 72
physicalDotsPerInch 72
physicalSize QSizeF(451.556, 361.244) (22.6')


// asus zenbook 13' without high dpi support
devicePixelRatio 1
geometry QRect(0,0 1366x768)
logicalDotsPerInch 96
physicalDotsPerInch 71.9833
physicalSize QSizeF(482, 271) (21.6'!)

// asus zenbook 13' with high dpi support
devicePixelRatio 1
geometry QRect(0,0 1366x768)
logicalDotsPerInch 96
physicalDotsPerInch 71.9833
physicalSize QSizeF(482, 271) (21.6'!)

It seems that situation becomes better for some Hight-DPI displays (Samsung tablet and Xiaomi Phone). 似乎某些Hight-DPI显示器(三星平板电脑和小米手机)的情况变得更好。 DPI of both devices become near 160 after High-DPI support applying. 在应用高DPI支持后,两种设备的DPI接近160。

But DPI of Retina display and low-density displays doesn't change and items on the screen looks bigger than it should be. 但Retina显示器和低密度显示器的DPI不会改变,屏幕上的项目看起来比应有的大。 So it solves only half of original problem. 所以它只能解决原问题的一半。 Maybe somebody knows how to manually set scale factor for all Qt application at runtime? 也许有人知道如何在运行时为所有Qt应用程序手动设置比例因子?

Seems like since Qt version 5.6 there might be a better solution for platform idependant scaling. 从Qt 5.6版开始,似乎可能有更好的解决方案来进行平台特定扩展。

High-DPI Support in Qt Quick Controls 2 https://doc.qt.io/qt-5/qtquickcontrols2-highdpi.html Qt Quick Controls中的高DPI支持2 https://doc.qt.io/qt-5/qtquickcontrols2-highdpi.html

I hope that helps. 我希望有所帮助。

I had the same problem and found the answer from jpnurmi most helpful: add 我有同样的问题,发现jpnurmi的答案最有帮助:添加

qputenv("QT_SCALE_FACTOR", "3"); 

in main() before creating the application instance. 在创建应用程序实例之前的main()中。 A factor of 0.75 worked very well for me for Retina displays, where the controls were actually too big. 对于Retina显示器来说,0.75的因子对我来说非常有效,其中控件实际上太大了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM