Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the DPI of the display screen in QT

Tags:

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.

like image 464
Arun Avatar asked Dec 16 '13 05:12

Arun


People also ask

What is physical DPI in Qt?

This class was introduced in Qt 5.0. A note on logical vs physical dots per inch: physical DPI is based on the actual physical pixel sizes when available, and is useful for print preview and other cases where it's desirable to know the exact physical dimensions of screen displayed contents.

What are the properties of a Qt screen?

The ratio between physical pixels and device-independent pixels for the screen. Common values are 1.0 on normal displays and 2.0 on Apple "retina" displays. This property was introduced in Qt 5.4. This contains the height of the screen in pixels. The manufacturer of the screen. This property was introduced in Qt 5.10. The model of the screen.

How to get the size of the screen in QQT?

Qt includes a very simple method to get the size of the screen ( width and height ). Here it is: QSize size = qApp->screens () [0]->size (); Note that this function works perfectly in Android and iOS too. Another thing to note is that if you replace [0] with 1, 2, 3, … you can access other monitors (if there are any) on desktop computers.

What is qscreen by default set to?

By default it is set to the value of the QScreen that the window uses. This property was introduced in Qt 5.4. The number of physical pixels per millimeter. This property was introduced in Qt 5.2. This contains the primary orientation of the screen.


2 Answers

In Qt 4.8 this seem to do the trick:

#include <QDesktopWidget>

...
int dpiX = qApp->desktop()->logicalDpiX();
...
like image 124
Anton Avatar answered Oct 18 '22 00:10

Anton


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!.

like image 45
Kuldeep Dhaka Avatar answered Oct 18 '22 01:10

Kuldeep Dhaka