Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. How to set application-wide font with Controls2 with dynamically created components

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

Scheduled Pinned Locked Moved Solved QML and Qt Quick
2 Posts 1 Posters 1.5k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    Eeli K
    wrote on last edited by Eeli K
    #1

    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.

    1 Reply Last reply
    0
    • E Offline
      E Offline
      Eeli K
      wrote on last edited by
      #2

      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".

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved