Need help on font.pointSize and font.pixelSize

3.5k Views Asked by At

This is my development environment.

Qt version: 5.9.4
Windows: Widows 10
Linux: Ubuntu 18.04

I have a requirement of setting the font size to 20.25. QML font.pixelSize takes an integer value, where as font.pointSize accepts real. So I decided to use font.pointSize. But using font.pointSize is showing some strange behavior.

import QtQuick 2.9
import QtQuick.Controls 2.2
import QtQuick.Window 2.2

Window {
    visible: true
    width: 800
    height: 480
    title: qsTr("Hello World")
    Column {
        spacing: 20
        Rectangle {
            height: 100
            width: 200
            Text {
                text: "Sample Text with font.pixelSize = 20"
                font {
                    pixelSize: 20
                }
            }
        }
        Rectangle {
            height: 100
            width: 200
            Text {
                text: "Sample Text with font.pointSize = 20"
                font {
                    pointSize: 20
                }
            }
        }
    }
}

With above code I am getting following output.
For me pixelSize is appearing correct according the value set. But pointSize is appearing very big.
on windows:
enter image description here

on Linux:
enter image description here

Note: in the above example, I intentionally used pointSize and pixelSize both to 20 to demonstrate the difference for same value. My requirement is anyway to use font size as 20.25.

Please let me know, if I am doing anything wrong here.

1

There are 1 best solutions below

0
On

Generally speaking, with a mixture of regular and high DPI displays, you should be using pointSize over pixelSize wherever possible. However, this requires that Qt actually has determine the DPI correctly. Unfortunately, it can get this wrong and yield the observations you make.

In such cases, you can override default behavior by changing it via the command line, e.g.

-platform windows:dpiawareness=0,1,2

Or via setting QT_SCALE_FACTOR environment variable prior to running your application.

For more information consult: https://doc.qt.io/qt-5/highdpi.html