【问题标题】:QMouseEvent::localPos() and high DPI display (Mac Retina)QMouseEvent::localPos() 和高 DPI 显示 (Mac Retina)
【发布时间】:2019-02-06 15:12:13
【问题描述】:

使用 Qt 5.12、Python 3、PySide2、MacOS。

mouseMoveEvent 处理程序中,QMouseEvent::localPos() 返回 QPointF,但仅限整数精度:小数点后的数字始终为零。

这意味着稍微不同的鼠标位置会产生位置数值完全相同的事件。

我希望当我将鼠标移动到稍微不同的位置时,例如:

事件1:(4.00, 8.00)

事件2:(4.50, 8.00)

但我明白了

事件1:(4.00, 8.00)

事件2:(4.00, 8.00)

在 Apple Retina 等高 DPI 显示器上,我如何区分仅略微不同位置的鼠标事件?

【问题讨论】:

    标签: qt pyside pyside2


    【解决方案1】:

    经过测试,QMouseEvent::windowPos() QMouseEvent::screenPos() 返回浮点精度。

    因此,我们可以通过mapTo(mainWindow, , QPoint(0, 0)) 获得localPos

    代码

    class MyWidget : public QWidget
    {
    public:
      ...
      MainWindow *getMainWindow() const { return mainWindow; }
      void setMainWindow(MainWindow *win) { mainWindow = win; }
    
    protected:
      void mouseMoveEvent(QMouseEvent *event) override
      {
        auto pos = event->windowPos();
        pos -= mapTo(getMainWindow(), QPoint(0, 0));
        qDebug() << event->localPos() << pos;
      }
    
    private:
      MainWindow *mainWindow;
    }
    

    结果

    QPointF(2,0) QPointF(2.46875,-0.179688)
    QPointF(3,0) QPointF(3.17188,0.171875)
    QPointF(3,0) QPointF(3.49609,0.496094)
    QPointF(4,1) QPointF(4.19922,0.847656)
    QPointF(5,1) QPointF(4.52344,1.17188)
    QPointF(5,1) QPointF(4.84766,1.49609)
    QPointF(5,2) QPointF(5.17188,1.82031)
    QPointF(5,2) QPointF(5.49609,2.14453)
    QPointF(6,2) QPointF(5.82031,2.46875)
    QPointF(6,3) QPointF(6.14453,2.79297)
    QPointF(6,3) QPointF(6.46875,3.11719)
    QPointF(7,3) QPointF(6.79297,3.44141)
    QPointF(7,4) QPointF(7.11719,3.76563)
    QPointF(7,4) QPointF(7.44141,3.76563)
    QPointF(8,4) QPointF(7.76563,4.08984)
    QPointF(8,4) QPointF(8.08984,4.41406)
    QPointF(9,5) QPointF(8.79297,4.76563)
    QPointF(9,5) QPointF(9.11719,5.08984)
    QPointF(9,5) QPointF(9.44141,5.41406)
    QPointF(10,6) QPointF(10.1445,6.11719)
    ...
    

    【讨论】:

    • 谢谢,这很有趣。这可能意味着问题出在 PySide2 Python 绑定中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-07
    相关资源
    最近更新 更多