Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Fonts render to integer widths on some platforms, render to real number on others



  • Hello,

    I've been trying to obtain a monospace font which renders to the same width per letter on any platform, measured in pixels. My current approach is to load a TrueType font and set a pixelWidth, like so:

    main.qml:

    Text {
    	id: mytext
    	text: 'a'
    	font.pixelSize: 18
    }
    Text {
    	id: widthmeasurement
    	text: mytext.width
    }
    

    main.cpp:

    QApplication app(argc, argv);
    QFontDatabase::addApplicationFont("TheFont.ttf");
    QFont font("TheFont");
    app.setFont(font);
    

    The issue is that on one platform (Win10), the width of each character is rounded to the next integer. On the other platform (Ubuntu 18.04), the width of each character is a real number. The result is an inconsistent text width between platforms.

    How can I get around this issue? Is there a setting to force integer widths for characters?

    Thank you!


Log in to reply