How to ensure the QFrame title is scaled automatically with the window's text size scaling?



  • I have a custom panel implementation that is inherited from QFrame. I can set the title by using the setWindowTitle() function. I noticed that I can not set the font size of this title, but I was expecting that this title will be scaled based on Windows 10's text scaling. I am using a 4K monitor and I am hoping the text can be scaled by windows setting, but that doesn't seem to be the case. I have set both QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling) and QCoreApplication::setAttribute(Qt::AA_UseHighDpiPixmaps) so the whole application and pixel images are scaled correctly.

    However, the title text of each custom panel is not affected by the Window 10's make text bigger setting, while the main application windows's title text is scaled properly and those text that is painted by using DrawText functions are scaled properly. However, my custom widget's title is not scaled properly.

    My custom widget is also loaded into a QDockWidget. I can modify the font size of the title directly but I am hoping it will be scaled properly like the application title bar.

    Is there anything I can do to have the title of the custom widget scaling based on the window 10 setting?

    A simplified code example to help you to understand the problem:
    My main application has a dockwidget and a custom 'panel' (inherited by a QFrame or QWidget) is attached to it. (See code below)

    QDockWidget* dock = new QDockWidget(panel->Title(), this);
    dock->setObjectName(panel->TypeId());
    dock->setAllowedAreas(panel->AllowedDockAreas());
    dock->setFeatures(dock->features() & ~QDockWidget::DockWidgetFloatable);
    dock->setWidget(panel);
    dock->setVisible(panel->DefaultVisible());
    addDockWidget(panel->DefaultDockArea(), dock);
    

    So if I add the following code, I can adjust the font size of the title directly but it is a manual change by myself. What I want is to have it automatically scaled based on the Windows's text scaling factor (e.g. 150%, 200% etc)

    QFont f = dock->font();
    f.setPointSizeF(f.pointSizeF()*devicePixelRatioF());
    dock->setFont(f);
    

    So my question is how to or whether it is possible to have the title of this custom panel to be scaled by the window 10's text scaling factor in control panel (especially in a high dpi situation)?

    Updates:
    It looks like the font and titlebar height are exactly the same between the custom panel and the main application. I also tried to set a WindowsFlag to the custom panel to be Qt::Desktop, so the panel becomes a standalone window and finally has its title text scaled with the Windows setting. So it looks like the Windows Manager is actually treating widget differently when painting it on the display. Maybe the only option is to have the custom titlebar for the dockwidget?


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    What version of Qt are you using ?



  • @SGaist I'm with 5.6.

    And interestingly, I used the following code to obtain the height of the title bar and surprisingly in any scale factor, the title bar of the application and the title bar of the custom widget gives the same value, despite they actually look different. These numbers do change when the text scaling factor changes. Does that mean it is actually how Window handles applications and its subsidiary widgets differently when dealing with scaling in display?

    auto dw_style = dock->style();
    int titleBarHeight = dw_style->pixelMetric(QStyle::PM_TitleBarHeight);
    int titleBarMargin = dw_style->pixelMetric(QStyle::PM_DockWidgetTitleMargin);

    auto main_style = this->style();
    int main_titleBarHeight = main_style->pixelMetric(QStyle::PM_TitleBarHeight);
    int main_titleBarMargin = main_style->pixelMetric(QStyle::PM_DockWidgetTitleMargin);



  • @SGaist To be more precisely, I'm with 5.6.1


  • Lifetime Qt Champion

    You might want to consider using a more recent version of Qt, the latest LTS is 5.12.6 and the current stable is 5.14.0. There's been a lot of work put into the high DPI support since Qt 5.6.



  • @SGaist I tried the Qt 5.12.6 and it did the trick! Thanks.

    But interestingly, the title of the main application is no longer scaled by "Make text bigger" setting but the dockwidget ones does. I guess that there is an intention not to scale the main title. "Make everything bigger" setting does scale everything properly now except the titlebar height of only the main application looks not enough for the enlarged font size. I will give qt5.14 a try as well.