How to set application-wide font with Controls2 with dynamically created components



  • Edit: this should be under QML subforum, sorry...

    I have a specific case with Controls2 application where setting the application font doesn't work. I have tried to set font size in main.cpp:

    QApplication* app{new QApplication(argc, argv)};
       QFont f{QGuiApplication::font()};
       f.setPointSize(QGuiApplication::font().pointSize()+4);
       QGuiApplication::setFont(f);
    

    In main.qml I create components dynamically in ApplicationWindow's Component.onCompleted:

    pageComponent = Qt.createComponent(pagename)
    pageComponent.createObject(mainPagesStackLayout)
    

    where the parent object is of course a stacklayout. The structure is basically this:

    ApplicationWindow{
        GridLayout{
            Item {
                StackLayout {
                    id: mainPagesStackLayout
    

    and a page is for example a subtype of Item or Flickable.

    So, setting the default font in main.cpp or in ApplicationWindow doesn't work. The pages use just some default font size, I don't know where it comes from. I can set the font for controls within each upper level control inside each page, but this feels stupid and requires manual coding in dozens of places. Is there a way to create these page components so that the font is inherited from ApplicationWindow? Using "pageComponent.createObject(mainPagesStackLayout, {'font': font})" doesn't work apparently because Item etc. don't have font property. For the same reason I can't set the font once per page which would otherwise be reasonable because I will have only maybe 5-10 pages.



  • I realized I can change the created components to Controls, either by changing the main level Item in the file to Control or by wrapping the main level type into a new Control. That way the font is inherited automatically. As a side note, contra the documentation ApplicationWindow didn't automatically use the font set by QGuiApplication::setFont. I had to add "font: Qt.application.font".


Log in to reply
 

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