'How to get the DPI of the display screen in Qt
I need to get the DPI value of display in Qt. I am able to get it in Qt 5.0 using the following:
#include <QScreen>
......
......
QScreen *srn = QApplication::screens().at(0);
qreal dotsPerInch = (qreal)srn->logicalDotsPerInch();
But the same code throws a compilation error in Qt version 4.x. My project is developed in Qt version 4.x. So I need the equivalent of the above code in Qt version 4.x.
Solution 1:[1]
In Qt 4.8 this seem to do the trick:
#include <QDesktopWidget>
...
int dpiX = qApp->desktop()->logicalDpiX();
...
Solution 2:[2]
Another way of getting information in Qt5:
QWidget contain physicalDpiX, physicalDpiY, logicalDpiY et cetera...
(QWidget inherit them from QPaintDevice.)
(thought the OP said Qt4, but Qt5 is in development currently.)
Story:
I was facing the same issue.
After @code_fodder answer (though in-complete but still deserve credit).
mentioned of QPaintDevice contain those relevant methods.
after reading more over, in noticed,
QWidget inherits QObject and QPaintDevice and when i saw, that was it!.
Solution 3:[3]
I think this is a Qt5 addition. For Qt4 or older (I think its supported in 3... but can't remember) you can use the QPaintDevice
to get similar information.
Here are the functions that will be useful to you depending what you need to do:
#include <QPaintDevice>
...
QPaintDevice paint;
int dpiX = paint.logicalDpiX();
int dpiY = paint.logicalDpiY();
int width = paint.widthMM();
int height = paint.heightMM();
Note: This is not an implementation, just example function calls.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Anton |
Solution 2 | |
Solution 3 | codeling |