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

Widgets added dynamically after show() uses font/scaling from primary screen

    XMLWordPrintable

Details

    • Windows

    Description

      This is in Qt 5, so I realize there is probably little interest in fixing it, but I hope to still get some help to fix it myself and perhaps other developers that run into this problem will find this useful.

      The problem is this:

      • You have a Windows-based desktop with multiple screens and the DPI/scaling varies across screens
      • You show a QWidget on a another screen than the primary screen and that screen has different DPI/scaling
      • You dynamically add a new child widget and that child widget is created without passing the parent

      A realistic scenario for this is that you have a QTabWidget where you dynamically create new tabs to represent say some form of document. If the widgets in the new tab are created with designer then all the widgets will generally be constructed without passing a parent. That is just how uic generates the code.

      The following example reproduces the problem:

      #include <QApplication>
      #include <QWidget>
      #include <QLineEdit>
      #include <QVBoxLayout>
      #include <QSpacerItem>
      #include <QPushButton>
      
      int main(int argc, char *argv[])
      {
          QApplication app(argc, argv);
      
          QWidget w;
          w.setWindowTitle("Example");
      
          const auto layout = new QVBoxLayout(&w);
          w.setLayout(layout);
      
          const auto line_edit = new QLineEdit(&w);
          line_edit->setText("Hello, World!");
          layout->addWidget(line_edit);
      
          const auto new_widgets_button = new QPushButton("Click me to add new widget!", &w);
          layout->addWidget(new_widgets_button);
      
          const auto spacer_item = new QSpacerItem(0, 0, QSizePolicy::Minimum, QSizePolicy::Expanding);
          layout->addItem(spacer_item);
      
          QObject::connect(new_widgets_button, &QPushButton::clicked, new_widgets_button, [&] {
              // NOTE: Scaling issue occurs when the new line edit is created without a parent here:
              const auto new_line_edit = new QLineEdit();
              new_line_edit->setText("Hello, World!");
              layout->insertWidget(layout->count() - 1, new_line_edit);
          });
      
          w.resize(1024, 768);
          // NOTE: Position the window on another screen than the primary screen to reproduce
          // the issue.
          w.move(-3012, 758);
          w.show();
      
          return app.exec();
      }
      

      As can be seen in the screenshot the dynamically added QLineEdit has wrong font size:

      Other widgets than QLineEdit also have problems when being dynamically added like this, but they are slightly different. For instance QLabel and QTabWidget tab headers calculate their sizes based on the wrong font size, but actually render using the correct font size. Look at the labels and tabs in the innermost widget in this more complicated screenshot:

      Attachments

        1. screenshot-1.png
          16 kB
          Thomas Sondergaard
        2. screenshot-2.png
          116 kB
          Thomas Sondergaard
        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            sorvig Morten Sørvig
            ts Thomas Sondergaard
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:

              Gerrit Reviews

                There are no open Gerrit changes