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

How set Native text renderer globaly?



  • Hello,
    I trying to set native render globaly for my qml app. I tried to use QQuickWindow#setTextRenderType. But there is a problem, that method should be called before main.qml load otherwise it won't work at all (app will use Qt renderer). So i tried to load temporal file, then use setTextRenderType and then load main.qml, as i see it is not a good solution and it is looking bad. So any idea how can i do this in better way? There is a small example of main c++

    #include <QGuiApplication>
    #include <QQmlApplicationEngine>
    #include <QQuickWindow>
    
    int main(int argc, char *argv[])
    {
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    
        QGuiApplication app(argc, argv);
    
        QQmlApplicationEngine engine;
    
        engine.load(QUrl(QStringLiteral("qrc:/qml/tempItem.qml")));
        QQuickWindow* window = qobject_cast<QQuickWindow*>(engine.rootObjects().first());
        window->setTextRenderType(QQuickWindow::TextRenderType::NativeTextRendering);
        engine.load(QUrl(QStringLiteral("qrc:/qml/main.qml")));
    
        if (engine.rootObjects().isEmpty())
            return -1;
    
        return app.exec();
    

    tempItem.qml

    import QtQuick 2.0
    Item { }
    

    main.qml

    import QtQuick 2.12
    import QtQuick.Window 2.12
    import QtQuick.Controls 2.12
    
    ApplicationWindow {
        visible: true
        width: 300
        height: 300
    
        Label {
              anchors.centeIn: parent
              text: "Hello world!"
        }
    }
    


  • The QQuickWindow::setTextRenderType() is a static method and sets the application-wide default text rendering method. You can call it in C++ code before engine.load() or even before your QQmlApplicationEngine object is instantiated.