Reputation: 2373
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 application look when DPI scaling size set to 100%
Qt application look when DPI scaling size set to 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.
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.
Upvotes: 49
Views: 72800
Reputation: 71
There are many ways.
qreal dpi = QGuiApplication::primaryScreen()->logicalDotsPerInch();
and then you calculate the new font size by multiplying it with the calculated ratio.
qreal refDpi = 216.;
qreal refHeight = 1776.;
qreal refWidth = 1080.;
QRect rect = QGuiApplication::primaryScreen()->geometry();
qreal height = qMax(rect.width(), rect.height());
qreal width = qMin(rect.width(), rect.height());
qreal dpi = QGuiApplication::primaryScreen()->logicalDotsPerInch();
m_ratio = qMin(height/refHeight, width/refWidth);
m_ratioFont = qMin(height*refDpi/(dpi*refHeight), width*refDpi/(dpi*refWidth));
you can use font property like that for example
font: bold italic large "Times New Roman";
Upvotes: 0
Reputation: 41
Here is a workaround:
Create a file qt.conf
and add these lines to it.
[Platforms]
WindowsArguments = dpiawareness=0
Put this file in the application binary folder. That's all. It will work well but only issue is that the look will not be that crisp. For delivering it to the customer, add this file where you have your dependency files & while creating setup, like normally you run your dependency files, run this same as well.
Upvotes: 4
Reputation: 33422
There are several options when dealing with high-resolution displays:
Sources:
Upvotes: 1
Reputation: 8273
High DPI support is enabled from Qt 5.6 onward.
Setting QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling)
in your application source code allows automatic high-DPI scaling.
NOTICE: To use the attribute method, you must set the attribute before you create your QApplication
object:
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QApplication app(argc, argv);
return app.exec();
}
Upvotes: 32
Reputation: 11046
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).
Here's how I got the dpi scale (in a file called "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()) );
}
Upvotes: 5
Reputation: 19112
Using layouts correctly can help.
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.
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
https://blog.qt.digia.com/blog/2009/06/26/improving-support-for-higher-dpi-on-vista/
You could try checking with other built in measurements from Qt:
http://qt-project.org/doc/qt-4.8/qpaintdevice.html#widthMM
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.
http://qt-project.org/doc/qt-4.8/qml-anchor-layout.html
QApplication
has some settings that are somewhat related.
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
Hope that helps.
Upvotes: 11