0

I have developed one android Application in Qt/QML,It is working well. but,when i change my Android phone setting eg.Display font->small to medium or medium to big. It is not applied in my application there is no change in the application. How to achieve this change in my application. Thanks in advance.

example:- if i change font size of display settings of android phone,then it automatically changes the font size of every app eq.whatsapp,facebook etc. but in my case, i have developed a android app in Qt/QML and it does not change with respect to display font size.

  • Please add more than this, for example show a piece of the code where you are using the font and setting the size, this way your question will likely get down voted/removed – Amfasis Jun 04 '20 at 12:02
  • @Amfasis i have updated my question,for the piece of code there is whole app(can't uplaod),means every UI of app should change with respect to it(display setting). – Surajkumar Tanurkar Jun 04 '20 at 12:37
  • Try solutions from this [thread](https://stackoverflow.com/questions/20464814/changing-dpi-scaling-size-of-display-make-qt-applications-font-size-get-rendere) – Ramkumar R Jun 04 '20 at 13:02
  • The only way I guess is to use Android API and then call C++/QML using Jni. Read how to [Handle configuration changes](https://developer.android.com/guide/topics/resources/runtime-changes). In your case you have to focus on `fontScale`. Check the [QAndroidJniObject](https://doc.qt.io/qt-5/qandroidjniobject.html) as well. Check [this](https://github.com/benlau/qtandroidexamplecode/tree/master/qtandroidrunner) article for the code examples. – folibis Jun 04 '20 at 13:16
  • @RamkumarR, I have gone through the thread,didn't get anything way out.Thank you for your suggestion. – Surajkumar Tanurkar Jun 04 '20 at 14:23
  • @folibis,Yes,You are correct i thought that,but i was developing the app purely in QML/Qt, i think it is not possible now.. – Surajkumar Tanurkar Jun 04 '20 at 14:25

2 Answers2

1

You have to do this manually and use C++ and JNI.

#include <QtAndroidExtras>

Then get the Java QtActivity class.

QAndroidJniObject qtActivity = QAndroidJniObject::callStaticObjectMethod("org/qtproject/qt5/android/QtNative", "activity", "()Landroid/app/Activity;");

Get the resources:

QAndroidJniObject resources = qtActivity.callObjectMethod("getResources","()Landroid/content/res/Resources;");

Get the configuration object and read the font scale.

QAndroidJniObject configuration = resources.callObjectMethod("getConfiguration","()Landroid/content/res/Configuration;");
float scale = configuration.getField<float>("fontScale");

Then set a QMLEngine context variable with the scale variable:

engine.rootContext()->setContextProperty("fontScale", scale);
Hubi
  • 1,629
  • 15
  • 20
0

@Hubi Hi,I tried this code in my qt/qml application. when i use this solution,it is working quite well,but the problem is that when i launch the app by default it is taking resource fontsize and setting that value to my UI code i tried is mentioned below.

#include "addnewguestclass.h"

AddNewGuestClass::AddNewGuestClass()
{

    QQmlApplicationEngine *m_View;
    m_View = new QQmlApplicationEngine();
    m_View->load(QUrl(QStringLiteral("qrc:test.qml")));
    //m_View->rootContext()->setContextProperty("AddNewGuest",this);
    //QObject *root = m_View->rootObjects().value(0);

    QAndroidJniObject qtActivity = QAndroidJniObject::callStaticObjectMethod("org/qtproject/qt5/android/QtNative", "activity", "()Landroid/app/Activity;");

    QAndroidJniObject resources = qtActivity.callObjectMethod("getResources","()Landroid/content/res/Resources;");

    QAndroidJniObject configuration = resources.callObjectMethod("getConfiguration","()Landroid/content/res/Configuration;");
    float scale = configuration.getField<float>("fontScale");
    qDebug()<<"Scale: "<< scale;

    m_View->rootContext()->setContextProperty("fontScale", scale);
}

main.qml

property real dp: screen.pixelDensity * 10 * 2.54 / 160
       Item {
           width: 50*dp
           height: 50*dp
           anchors.centerIn: parent

           Label {
               text: qsTr("Hi, I am Hemant")
               //font.pixelSize: 16*dp
               font.pixelSize: Qt.application.font.pixelSize

           }
       }