0

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.

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. Other application like Visual Studio have their label and text scaled by the "Make text bigger setting" on top of the Display setting's scaling.

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 fix this?

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)?

Update: 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 out 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 sees applications and its subsidiary widgets differently when dealing with scaling?

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

std::cout << "Panel=" << panel->Title().toStdString() << ", titleBarHeight=" << titleBarHeight << std::endl;
std::cout << "Panel=" << panel->Title().toStdString() << ", titleBarMargin=" << titleBarMargin << std::endl;

auto main_style = this->style();
int main_titleBarHeight = main_style->pixelMetric(QStyle::PM_TitleBarHeight);
int main_titleBarMargin = main_style->pixelMetric(QStyle::PM_DockWidgetTitleMargin);
std::cout << "TMainForm=" << this->windowTitle().toStdString() << ", main_titleBarHeight=" << main_titleBarHeight << std::endl;
std::cout << "TMainForm=" << this->windowTitle().toStdString() << ", main_titleBarMargin=" << main_titleBarMargin << std::endl;

Update2:

I got a reasonably good solution from the Qt forum as the later version of Qt5 (Qt5.12.6) solves the issue for me (as I was on Qt5.6.1). Apparently a lot of improvement has been made to help usage in high dpi monitor environment. But interestingly in the newer update, Qt decides to scale the title of the widget but not the main application title.

The solution is definitely something to do with how to create a widget that works with Windows Manager properly if I want to manually make it happen and certainly not sure whether I can do that in Qt environment. The QDockwidget let us set custom titlebar, which might be the only way to bypass the existing setting in older Qt version.

John Yang
  • 547
  • 1
  • 8
  • 21
  • @eyllansec it's pretty hard to provide an example for this as the scale of the code is pretty big. But what I can say is that with a high DPI monitor, if you enable high dpi scaling, do you get the title of your custom widget in a QDockWidget scaled properly like the title bar of the main application when the scale factor of the text in windows 10 control panel changed? – John Yang Jan 06 '20 at 04:59
  • @eyllanesc Sure. But I think if I'm going to provide a minimal example, I think my description in the above comment states all. I just need a way to have the title bar of a custom widget docked in a qdockwidget to be scaled automatically with the windows text scale factor. I can't get that to happen. I will add some code in the question but I don't think it's a bug rather than something to be added if you know the answer. or maybe it can't be achieved unless with a custom title bar? – John Yang Jan 06 '20 at 05:07

0 Answers0