Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-68087

Can't specify font for custom style

    XMLWordPrintable

Details

    • Bug
    • Resolution: Unresolved
    • P2: Important
    • None
    • 5.11
    • Quick: Controls 2
    • None

    Description

      I have a custom style that only provides Button.qml. The style should have a custom font, which is specified in the qtquickcontrols2.conf file:

      qtquickcontrols2.conf
      [Controls]
      Style=:/style
      FallbackStyle=Fusion
      
      [:/style]
      ;[style]
      Font\Family=Swis721 Cn BT D-Type
      Font\PixelSize=20
      

      The font is loaded in C++:

      main.cpp
      #include <QGuiApplication>
      #include <QQmlApplicationEngine>
      #include <QFontDatabase>
      #include <QDebug>
      
      int main(int argc, char *argv[])
      {
          QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
      
          QGuiApplication app(argc, argv);
      
          const int fontId = QFontDatabase::addApplicationFont(":/Swis721 Cn BT D-Type.ttf");
          if (fontId == -1) {
              qWarning() << "Failed to load application font!";
          } else {
              QFont font("Swis721 Cn BT D-Type", 10);
              QGuiApplication::setFont(font);
          }
      
          QQmlApplicationEngine engine;
          engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
          if (engine.rootObjects().isEmpty())
              return -1;
      
          return app.exec();
      }
      

      However, the custom Button uses the wrong font:

      style/Button.qml
      import QtQuick 2.9
      import QtQuick.Templates 2.2 as T
      
      T.Button {
          id: control
          implicitWidth: 200
          implicitHeight: 40
      
          contentItem: Text {
              text: control.text
              font: control.font
              color: "white"
          }
      
          background: Rectangle {
              color: "#444"
          }
      }
      

      Attachments

        Issue Links

          Activity

            People

              qt.team.quick.subscriptions Qt Quick and Widgets Team
              mitch_curtis Mitch Curtis
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

                Created:
                Updated: