在Qt中获取物理屏幕尺寸 [英] Get physical screen size in Qt
问题描述
我正在Qt工作,我需要帮助来获取屏幕(显示器)的物理尺寸,
I'km working in Qt, i need help to get the physical size of the screen (monitor),
在Qt中,可以从QApplication
获取QDesktopWidget
,我的意思是:
In Qt one can get a QDesktopWidget
from QApplication
, I mean:
QDesktopWidget *mydesk = QApplication::desktop();
QDesktopwidget
有一些方法可以获取以像素为单位的分辨率,而另一些方法可以获取以milimethers为单位的尺寸:
The QDesktopwidget
has some methods to get the resolution in pixels and some to get the the size in milimethers:
mydesk-> widthMM(); mydesk->heightMM();
但是,这与物理尺寸不符,当我用尺子测量屏幕时,会有很大的差异.
However, this does not correspond to the physical size, when I measure my screen with a ruler, there is a considerable difference.
也可以获取DPI测量值并计算屏幕尺寸:
Also one can get the DPI measurement and calculate the size of the screen:
mydesk->physicalDpiX(); mydesk->physicalDpiY();
double Winches = (double)mydesk.width() / (double)mydesk.physicalDpiX();
double Hinches = (double)mydesk.Height() / (double)mydesk.physicalDpiY();
其中mydesk.width()
和mydesk.height()
给出像素大小(分辨率)
where mydesk.width()
and mydesk.height()
give the size in pixels(resolution)
但是测量值也是错误的,非常接近mydesk.widthMM()
和mydesk.heightMM()
However the measurement is also wrong and very close to mydesk.widthMM()
and mydesk.heightMM()
我也尝试过mydesk.logicalDpiX()
,它的结果类似.
Also I have triyed mydesk.logicalDpiX()
and it has similar results.
推荐答案
这是我的示例(快速又肮脏).它似乎对我有用,我希望它对您有用.我假设您可以自己维护main.cpp.我是在MacBook Air 11.6上完成此操作的,然后用一角硬币的图片替换了OS X随附的USA图标:
Here is my (quick and dirty) example. It seems to work for me and I hope it works for you. I'm assuming you can take care of main.cpp on your own. I did this on a MacBook Air 11.6" and substituted a picture of a dime for the USA icon included with OS X:
#ifndef WINDOW_H
#define WINDOW_H
#include <QtGui>
class Window : public QWidget
{
Q_OBJECT
public:
QWidget *canvas;
QSlider *slider;
QPixmap pixmap;
private:
qreal zoom;
qreal pixels;
qreal px_width;
qreal px_height;
qreal mm_width;
qreal mm_height;
public:
Window();
void paintEvent(QPaintEvent *);
public slots:
void setZoom(int);
};
Window::Window()
{
QHBoxLayout *layout = new QHBoxLayout;
canvas = new QWidget;
slider = new QSlider;
slider->setMinimum(0);
slider->setMaximum(100);
slider->setValue(50);
layout->addWidget(canvas);
layout->addWidget(slider);
this->setLayout(layout);
if(!pixmap.load(":/resources/USA.gif"))
{
qDebug() << "Fatal error: Unable to load image";
exit(1);
}
QObject::connect(slider, SIGNAL(valueChanged(int)), SLOT(setZoom(int)));
}
void Window::paintEvent(QPaintEvent *event)
{
QPainter paint;
paint.begin(this);
paint.scale(zoom, zoom);
paint.drawPixmap(0,0, pixmap);
paint.end();
}
void Window::setZoom(int new_zoom)
{
zoom = (qreal)(50+new_zoom) /50;
pixels = pixmap.width() * zoom;
QDesktopWidget desk;
px_width = desk.width() / pixels;
px_height = desk.height() / pixels;
mm_width = px_width * 17.9;
mm_height = px_height * 17.9;
qDebug() << "Zoom: " << zoom;
qDebug() << "desk.widthMM:" << desk.widthMM();
qDebug() << "px_width: " << px_width;
qDebug() << "px_height: " << px_height;
qDebug() << "mm_width: " << mm_width;
qDebug() << "mm_height: " << mm_height;
this->repaint();
}
#include "moc_window.cpp"
#endif // WINDOW_H
这篇关于在Qt中获取物理屏幕尺寸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!