How to force font ?



  • I have child widget like this:

    LWidget(QQmlEngine *eng, QWidget *parent = nullptr) :QQuickWidget(eng, parent),

    So I want to know how to setup default font family from C++ side to any loaded QML (which does not have explicit family set) ?

    This test piece from constructor does nothing:

    QFont f = QApplication::font();
    //f.setFamily("Arial");
    f.setFamily("Serto Jerusalem Outline");
    setFont(f);



  • Solved meself:

    void updateFont(const QString& family, QQuickItem *root)
    {
    QVariant fn = root->property("font");
    if (fn.isValid() && fn.canConvert<QFont>())
    {
    QFont fn2 = fn.value<QFont>();
    fn2.setFamily(family);
    root->setProperty("font", fn2);
    }
    for (const auto& c : root->childItems())
    updateFont(family, c);
    }

    Usage:

    setSource(url);
    updateFont("Serto Jerusalem Outline", rootObject());

    However, it will override any explicit fonts too, but its ok for me.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.