在Qt中获取物理屏幕尺寸

发布于 2024-12-22 03:41:54 字数 890 浏览 2 评论 0原文

我在 Qt 中工作,我需要帮助来获取屏幕(监视器)的物理尺寸,

在 Qt 中,可以从 QApplication 获取 QDesktopWidget,我的意思是

QDesktopWidget *mydesk = QApplication::desktop();

QDesktopwidget 有一些方法可以获取以像素为单位的分辨率,还有一些方法可以获取以毫米为单位的尺寸:

mydesk-> widthMM(); mydesk->heightMM();

但是,这与物理尺寸不对应,当我用尺子测量屏幕时,有一个大量 不同之处。

另外,我们还可以获取 DPI 测量值并计算屏幕的大小:

mydesk->physicalDpiX(); mydesk->physicalDpiY();

double Winches = (double)mydesk.width() / (double)mydesk.physicalDpiX();
double Hinches = (double)mydesk.Height() / (double)mydesk.physicalDpiY();

其中 mydesk.width()mydesk.height() 给出以像素为单位的大小(分辨率)

但是测量结果也是错误的,并且非常接近 mydesk.widthMM()mydesk.heightMM()

我也尝试过 mydesk.ologicalDpiX()它也有类似的结果。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

丶视觉 2024-12-29 03:41:54

这是我的(快速而肮脏的)示例。它似乎对我有用,我希望它对你有用。我假设您可以自己处理 main.cpp 。我在 MacBook Air 11.6" 上执行了此操作,并用一毛钱的图片替换了 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

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
神经暖 2024-12-29 03:41:54

以下方法可以帮助我获得屏幕的准确物理尺寸(以毫米为单位)。

QSizeF screenSize = QGuiApplication::primaryScreen()->physicalSize();

然而,在Qt文档中我发现了下面这句话:

取决于底层系统提供什么信息的值
可能不完全准确。

所以它可能不适用于您的系统。

The following method worked for me to get accurate physical size of my screen in mm.

QSizeF screenSize = QGuiApplication::primaryScreen()->physicalSize();

However, in Qt documentation I found the following sentence:

Depending on what information the underlying system provides the value
might not be entirely accurate.

So it might not work for your system.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文