OverviewFelgo 2 Support (Qt 5) › How to properly implement Felgo QML sp() function in C++

Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
  • #19398


    I am writing some text in a filter over a QVideoFrame in C++ and I would like that the font size automatically adapts to different screen sizes and pixel densities. I think this would be the implementation in C++ of the QML sp() function.

    // Dots per inch
    double dpi()
      return QApplication::screens().count() > 0 ? QApplication::screens().first()->physicalDotsPerInch() : 160;
    // sp function
    double sp(double pixel)
       return pixel * dpi() / 160;

    Then, I just set a reference value for pixel size and call this function to set the font pixel size, as follows.

    const double FONT_PIXEL_SIZE = 28;
    QFont font;

    However, if I do that I get a font size much bigger on Android than on iOS. What am I missing here?



    Felgo Team


    if you have the QML App/Window item available in C++, you should also be able to call the App::sp() function from your C++ code to get the same values as in your QML code.

    For you custom version of the sp function, you can also have a look at the QGuiApplication::devicePixelRatio(). You can the define the sp function as:

    return pixel * dpi() / 160 / app.devicePixelRatio()




    Thanks Günther!



    Hi Günther,

    I have being testing and still, I do not get it. By the way, I think the code should be the following, right?.

    return pixel * dpi() / (160 / app.devicePixelRatio())

    Anyway, I do not get the font size right. I tested this on a Samsung Galaxy S8 because I can change the screen resolution.

    If I set the screen resolution to HD+ (1480×720) I get this

    and If I set the screen resoultion to WQHD+ (2960×1440) I get this

    Notice that the text “Live” pixel size, rendered by Felgo, is right, so for sure, I am missing something.

    Any ideas?, thanks.


    Felgo Team


    in Felgo we use this calculation:

    // dp function
    return pixel * densityScaleFactor
    // scale factor
    property real densityScaleFactor: (screenDpi / 160) / app.devicePixelRatio()




    Thanks Günther,

    Let see if I can get this right.



    Hi Gúnther,

    It is still not clear to me, let’s consider this C++ code to get some information about an iPad Mini 4 and iPhone 7.

        qDebug() << "Physical DPI:" << QApplication::screens().first()->physicalDotsPerInch();
        qDebug() << "Logical DPI:"  << QApplication::screens().first()->logicalDotsPerInch();
        qDebug() << "Pixel ratio:"  << qApp->devicePixelRatio();

    I get the same results for both devices. I use the Physical DPI as screenDPI.

    Physical DPI: 163
    Logical DPI: 72
    Pixel ratio: 2

    Since the iPad Mini 4 resolution is 2048×1536 and the iPhone 7 resolution is 1334×750 and the value returned by the sp function is the same on both devices, i.e same font pixel size, I get texts with different sizes on each device.


    Felgo Team

    Hi Javier,

    to fix your issues, we have to look at your source-code and use-case in detail. We can do that with the purchase of a support package or the included support of Felgo Indie. Just let us know at if this is interesting for you. Regarding usage of density and screen features with Qt, you can also try to get support from other Qt devs in the official Qt forums.




    Thanks Günther,

    Yes, this issue is kind of specific to my app. I will consider the options you mentioned.


Viewing 9 posts - 1 through 9 (of 9 total)

RSS feed for this thread

You must be logged in to reply to this topic.

Voted #1 for:

  • Easiest to learn
  • Most time saving
  • Best support

Develop Cross-Platform Apps and Games 50% Faster!

  • Voted the best supported, most time-saving and easiest to learn cross-platform development tool
  • Based on the Qt framework, with native performance and appearance on all platforms including iOS and Android
  • Offers a variety of plugins to monetize, analyze and engage users
create apps
create games
cross platform
native performance
3rd party services
game network
level editor
easiest to learn
biggest time saving
best support