qt 获取dpi_在Qt中获取物理屏幕尺寸

在Qt中,通过QDesktopWidget获取的屏幕尺寸并不对应于实际物理尺寸。本文介绍了通过结合DPI获取更准确的物理屏幕尺寸的方法,包括使用QDesktopWidget的widthMM(), heightMM(), physicalDpiX()和physicalDpiY()等方法,并提供了一个简单的示例代码,展示了如何计算和调整屏幕的毫米宽度和高度。" 65632548,6445291,HFSS仿真教程:高速差分过孔信号完整性的实现,"['HFSS', '信号完整性', '仿真软件', 'PCB设计', '电磁仿真']

I'km working in Qt, i need help to get the physical size of the screen (monitor),

In Qt one can get a QDesktopWidget from QApplication, I mean:

QDesktopWidget *mydesk = QApplication::desktop();

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.

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();

where mydesk.width() and mydesk.height() give the size in pixels(resolution)

However the measurement is also wrong and very close to mydesk.widthMM() and mydesk.heightMM()

Also I have triyed mydesk.logicalDpiX() and it has similar results.

解决方案

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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值