简体   繁体   English

你如何将鼠标坐标转换为“mm”

[英]How do you translate mouse coordinates into "mm"

I am trying to convert the distance from a mouse press to mouse release into mm.我正在尝试将鼠标按下到鼠标释放的距离转换为 mm。 I already calculated the distance but am not sure how to convert to the correct units.我已经计算了距离,但不确定如何转换为正确的单位。

void Widget::mousePressEvent(QMouseEvent* event)
{
    if (event->buttons() & Qt::LeftButton)
    {
        xAtPress = event->x();
        yAtPress = event->y();
        qDebug() << "Mouse X press: " << xAtPress;
        qDebug() << "Mouse Y press: " << yAtPress;
    }
}
void Widget::mouseReleaseEvent(QMouseEvent* event)
{
    if (event->button() == Qt::LeftButton) 
    {
        int xAtRelease = event->x();
        int yAtRelease = event->y();
        qDebug() << "Mouse X release: " << xAtRelease;
        qDebug() << "Mouse Y release: " << yAtRelease;
        int dx = xAtRelease - xAtPress;
        int dy = yAtRelease - yAtPress;
        qDebug() << "Dx: " << dx;
        qDebug() << "Dy: " << dy;


        //Create some kind of conversion function here.


    }
}

In short:简而言之:
You can't (at least not accurately and reliably).你不能(至少不能准确可靠)。

In long:长:
In order to perform even the most basic conversion you need to know the conversion factor between pixels and mm.为了执行最基本的转换,您需要知道像素和毫米之间的转换系数。 And in order to calculate this factor you need to know the resolution of the screen (eg via QScreen ) and the real size of the screen - which is is the problematic part.并且为了计算这个因素,您需要知道屏幕的分辨率(例如通过QScreen )和屏幕的实际尺寸 - 这是有问题的部分。 QScreen even provides a physicalSize() function which returns the size of the screen (in mm) - but then warns QScreen甚至提供了一个physicalSize()函数,该函数返回屏幕的大小(以毫米为单位)-但随后发出警告

Depending on what information the underlying system provides the value might not be entirely accurate.根据底层系统提供的信息,该值可能不完全准确。

which essentially means: In the best case your monitor correctly provides that information and your OS correctly handles that information so you will get sensible data;这基本上意味着:在最好的情况下,您的显示器正确提供该信息,并且您的操作系统正确处理该信息,因此您将获得合理的数据; more likely you will get utter nonsense;更有可能你会完全胡说八道; worst case you get values that look plausible - but actually are subtly incorrect.最坏的情况是你得到的值看起来似乎合理——但实际上是微妙的不正确。

In essence there is no reliable way to query that information from the OS, so you can't do it.从本质上讲,没有可靠的方法可以从操作系统查询该信息,因此您不能这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM