How to get the DPI of the display screen in QT

2020-07-10 06:09发布

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 error in QT 4.x version. My project developed in QT 4.x version. So I need the equivalent of the above code in QT 4.x version.

标签: qt
3条回答
Explosion°爆炸
2楼-- · 2020-07-10 06:25

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

查看更多
淡お忘
3楼-- · 2020-07-10 06:27

In Qt 4.8 this seem to do the trick:

#include <QDesktopWidget>

...
int dpiX = qApp->desktop()->logicalDpiX();
...
查看更多
别忘想泡老子
4楼-- · 2020-07-10 06:33

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.

查看更多
登录 后发表回答