C++ 更改显示的 DPI 缩放大小会使 Qt 应用程序的字体大小变得更大
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20464814/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Changing DPI scaling size of display make Qt application's font size get rendered bigger
提问by Arun
I have created some GUI application using Qt. My GUI application contains controls like push button and radio button. When I run the application, buttons and fonts inside button looks normal. When I change the DPI scaling size of display from 100% to 150% or 200%, font size of controls rendered bigger but not control size (pushbutton, radio button) irrespective of resolution. Due to this the text inside controls were cut off. please see the attached image.
我使用 Qt 创建了一些 GUI 应用程序。我的 GUI 应用程序包含按钮和单选按钮等控件。当我运行应用程序时,按钮内的按钮和字体看起来很正常。当我将显示的 DPI 缩放大小从 100% 更改为 150% 或 200% 时,无论分辨率如何,控件的字体大小都会变大,但控件大小(按钮、单选按钮)不会。由于这个原因,控件中的文本被截断了。请参阅附图。
Qt application look when DPI scaling size set to 100%
DPI 缩放大小设置为 100% 时的 Qt 应用程序外观
Qt application look when DPI scaling size set to 200%
Qt 应用程序在 DPI 缩放大小设置为 200% 时的外观
I am running my application in some tablets also. In tablets, DPI scale value should be more than 150% else everything will be shown very small.
我也在一些平板电脑上运行我的应用程序。在平板电脑中,DPI 比例值应大于 150%,否则所有内容都会显示得非常小。
I searched in the web for creating UI application in Qt irrespective of resolution and DPI scale value but no luck. So I am posting my questing here. Please let me know if there is some way to get rid of this.
我在网上搜索在 Qt 中创建 UI 应用程序,而不管分辨率和 DPI 比例值,但没有运气。所以我在这里发布我的任务。请让我知道是否有办法摆脱这种情况。
回答by Nicolas Holthaus
High DPI support is enabled from Qt 5.6 onward.
从 Qt 5.6 开始启用高 DPI 支持。
Setting QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling)
in your application source code allows automatic high-DPI scaling.
QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling)
应用程序源代码中的设置允许自动高 DPI 缩放。
NOTICE: To use the attribute method, you must set the attribute before you create your QApplication
object:
注意:要使用属性方法,您必须在创建QApplication
对象之前设置属性:
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QApplication app(argc, argv);
return app.exec();
}
回答by phyatt
Using layouts correctly can help.
正确使用布局会有所帮助。
http://qt-project.org/doc/qt-4.8/layout.html
http://qt-project.org/doc/qt-4.8/layout.html
Telling the OS that you handle DPI changes, will prevent weird font changes that you weren't expecting.
告诉操作系统您处理 DPI 更改,将防止您没有预料到的奇怪字体更改。
http://msdn.microsoft.com/en-us/library/ms701681(v=vs.85).aspx
http://msdn.microsoft.com/en-us/library/ms701681(v=vs.85).aspx
For spacing critical places, you can check the size of your rendered font, and then set the minimum size of your object based on the resulting size of your text.
对于间隔关键位置,您可以检查渲染字体的大小,然后根据文本的结果大小设置对象的最小大小。
http://qt-project.org/doc/qt-4.8/qfontmetrics.html#details
http://qt-project.org/doc/qt-4.8/qfontmetrics.html#details
https://blog.qt.digia.com/blog/2009/06/26/improving-support-for-higher-dpi-on-vista/
https://blog.qt.digi.com/blog/2009/06/26/improving-support-for-higher-dpi-on-vista/
You could try checking with other built in measurements from Qt:
您可以尝试检查来自 Qt 的其他内置测量值:
http://qt-project.org/doc/qt-4.8/qpaintdevice.html#widthMM
http://qt-project.org/doc/qt-4.8/qpaintdevice.html#widthMM
http://qt-project.org/doc/qt-4.8/qpaintdevice.html#logicalDpiX
http://qt-project.org/doc/qt-4.8/qpaintdevice.html#logicalDpiX
If you are using QML, try for pristine layouts of only anchor based placement.
如果您使用 QML,请尝试仅基于锚点放置的原始布局。
http://qt-project.org/doc/qt-4.8/qml-anchor-layout.html
http://qt-project.org/doc/qt-4.8/qml-anchor-layout.html
QApplication
has some settings that are somewhat related.
QApplication
有一些相关的设置。
http://qt-project.org/doc/qt-4.8/qapplication.html#setDesktopSettingsAware
http://qt-project.org/doc/qt-4.8/qapplication.html#setDesktopSettingsAware
You could manually specify the font, too.
您也可以手动指定字体。
http://qt-project.org/doc/qt-4.8/qapplication.html#setFont
http://qt-project.org/doc/qt-4.8/qapplication.html#setFont
Hope that helps.
希望有帮助。
回答by BuvinJ
I had a fixed size window which was not large enough to fit all the text it contained when Windows accessibility settings where applied to scale up all text sizes. Windows does this via dpi increases. I fixed this by retreiving the os scaling factor and then adjusted the size of the my window and some of it's layouts (which I couldn't get to scale automatically for some reason).
我有一个固定大小的窗口,当 Windows 辅助功能设置应用于放大所有文本大小时,它不足以容纳它包含的所有文本。Windows 通过 dpi 增加来做到这一点。我通过检索 os 缩放因子解决了这个问题,然后调整了我的窗口的大小和它的一些布局(由于某种原因我无法自动缩放)。
Here's how I got the dpi scale (in a file called "WindowsDpiScale.h"):
这是我获得 dpi 比例的方法(在名为“WindowsDpiScale.h”的文件中):
#ifndef WINDOWSDPISCALE_H
#define WINDOWSDPISCALE_H
#include <QtGlobal>
#ifdef Q_OS_WIN
#include <windows.h>
const float DEFAULT_DPI = 96.0;
float windowsDpiScale()
{
HDC screen = GetDC( 0 );
FLOAT dpiX = static_cast<FLOAT>( GetDeviceCaps( screen, LOGPIXELSX ) );
ReleaseDC( 0, screen );
return dpiX / DEFAULT_DPI;
}
#endif //Q_OS_WIN
#endif // WINDOWSDPISCALE_H
And then, how I applied it in my case:
然后,我如何在我的案例中应用它:
...
#include "WindowsDpiScale.h"
MainWindow::MainWindow( QWidget *parent )
: QMainWindow( parent )
{
...
// Enlarge the window and various child widgets to accomendate
// OS display scaling (i.e. accessibily options)
setScaleToOsSettings();
...
}
void MainWindow::setScaleToOsSettings()
{
#ifdef Q_OS_WIN
setScale( windowsDpiScale() );
#endif
}
void MainWindow::setScale( float scale )
{
// Resize the window
this->setFixedSize( (int)(scale * this->maximumWidth()),
(int)(scale * this->maximumHeight()) );
// Resize the layouts within the stacked widget
foreach( QVBoxLayout * layout,
windowUi_->pagerStackedWidget->findChildren<QVBoxLayout *>() )
layout->parentWidget()->setFixedSize(
(int)(scale * layout->parentWidget()->contentsRect().width()),
(int)(scale * layout->parentWidget()->contentsRect().height()) );
}