Learn what Felgo offers to help your business succeed. Start your free evaluation today! Felgo for Your Business

Forums

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

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

    Javier

    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;
    
    font.setPixelSize(sp(FONT_PIXEL_SIZE));

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

     

    #19400

    Günther
    Felgo Team

    Hi,

    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()

    Best,
    Günther

    #19427

    Javier

    Thanks Günther!

    #19532

    Javier

    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 https://drive.google.com/open?id=19uY2nq4rhTUiy9s2_DMUq8zV-29rpdtt

    and If I set the screen resoultion to WQHD+ (2960×1440) I get this https://drive.google.com/open?id=1ZvR8xlZ1YmpqW7z8j3l-IBXPJw7jF7L1

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

    Any ideas?, thanks.

    #19538

    Günther
    Felgo Team

    Hi,

    in Felgo we use this calculation:

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

    Best,
    Günther

    #19540

    Javier

    Thanks Günther,

    Let see if I can get this right.

    #19566

    Javier

    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.

    #19618

    Günther
    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 support@felgo.com 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.

    Best,
    Günther

    #19619

    Javier

    Thanks Günther,

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

    Regards,
    Javier.

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

RSS feed for this thread

You must be logged in to reply to this topic.

Qt_Technology_Partner_RGB_475 Qt_Service_Partner_RGB_475_padded