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

The default font resolution of a QWidget subclass is random w.r.t. QApplication::font

    XMLWordPrintable

Details

    • All
    • dafd26acbe7b08f5ddfe60432fe0e49422ac085c (qt/qtbase/dev) c00f52e821a3a982e91d9c4515c3d4d944305a16 (qt/qtbase/6.0) 80368a9fb9ed94349360cc6292b1a1a57e7bf297 (qt/tqtc-qtbase/tqtc/lts-5.15)

    Description

      In some cases a QWidget subclass does no resolve the correct default font w.r.t. QApplication::font. This seems to happen in inheritance chain at least 3 levels deep (i.e. subclass of a subclass of QWidget where all classes are using Q_OBJECT macro and there are different fonts registered for the widget classes in the inheritance chain (in QApplication::font). In this case the widget inherit one of the registered fonts not necessarily the one from the most immediate ancestor.

      E.g. On macos the QHeaderView has a smaller default font then the QWidget. A subclass of QHeaderView could on some runs inherit the QHeaderView's font or just the default QWidget's.

      The problem seems to be in QApplication::font(const QWidget *) implementation where the search for the class name registered font traverses an unordered container (QHash), breaking on the first entry that matches any of the query widget's base classes, where it should match its most specific super class that has an entry.

      Attached is an example project with a QHeaderView subclass displayed side by side with base QHeaderView (in both c++ and python). Note that it must be run multiple times for the difference to show.

      headerview.h

      #ifndef HEADERVIEW_H
      #define HEADERVIEW_H
      
      #include <QHeaderView>
      
      /*
       * A QHeaderView subclass with a new QMetaObject className.
       *
       */
      class HeaderView: public QHeaderView {
          Q_OBJECT
      public:
          explicit HeaderView(Qt::Orientation orientation, QWidget *parent=nullptr):
              QHeaderView(orientation, parent) {}
          virtual ~HeaderView() {}
      };
      
      #endif // HEADERVIEW_H
      

      main.cpp

      #include <QApplication>
      #include <QStandardItemModel>
      #include <QDebug>
      #include <QVBoxLayout>
      #include "headerview.h"
      
      
      int main(int argc, char *argv[])
      {
          QApplication a(argc, argv);
          QFont font;
          // Set bold font for QHeaderView to make the difference more noticable
          // even on systems where there are no differences in default fonts
          // (note on macos QHeaderView has smaller font by default and this behaviour
          // is observable without any setFont calls)
          font.setBold(true);
          font.setPointSizeF(20.0);
          a.setFont(font, "QHeaderView");
      
          // Explicitly set a font for each class in the QHeaderView's base class chain.
          // This increases the incidence of the issue.
          QFont basefont;
          basefont.setBold(false);
          font.setPointSizeF(10.0);
          a.setFont(basefont, "QAbstractItemView");
          a.setFont(basefont, "QAbstractScrollView");
          a.setFont(basefont, "QFrame");
      
          QWidget container;
          auto layout = new QVBoxLayout();
          layout->setContentsMargins(0, 0, 0, 0);
          layout->setSpacing(0);
          container.setLayout(layout);
      
          auto model = new QStandardItemModel();
          model->setColumnCount(10);
      
          auto* header1 = new QHeaderView(Qt::Horizontal);
          auto* header2 = new HeaderView(Qt::Horizontal);
      
          qDebug() << "HeaderView inherits bold font:"<< QApplication::font(header2).bold();
      
          header1->setModel(model);
          header2->setModel(model);
          header1->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Ignored);
          header2->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Ignored);
          layout->addWidget(header1);
          layout->addWidget(header2);
          container.resize(200, 50);
          container.show();
          return a.exec();
      }
      

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            qt.team.quick.subscriptions Qt Quick and Widgets Team
            aleserjavec Ales Erjavec
            Votes:
            0 Vote for this issue
            Watchers:
            6 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes