• Qt全局坐标和相对坐标


    QMouseEvent中两类坐标系统,一类是窗口坐标,一类是显示器坐标。

    QPoint QMouseEvent::pos() 返回相对这个widget(重载了QMouseEvent的widget)的位置
    QPoint QMouseEvent::globalPos() 窗口坐标,这个是返回鼠标的全局坐标
    QPoint QCursor::pos() [static] 返回相对显示器的全局坐标
    QPoint QWidget::mapToGlobal(const QPoint & pos)  const 将窗口坐标转换成显示器坐标
    QPoint QWidget::mapFromGlobal(const QPoint & pos) const 将显示器坐标转换成窗口坐标
    QPoint QWidget::mapToParent(const QPoint & pos) const 将窗口坐标获得的pos转换成父类widget的坐标
    QPoint QWidget::mapFromParent(const QPoint & pos) const 将父类窗口坐标转换成当前窗口坐标
    QPoint QWidget::mapTo(const QWidget * parent, const QPoint & pos) const 将当前窗口坐标转换成指定parent坐标
    QWidget::pos() : QPoint 这个属性获得的是当前目前控件在父窗口中的位置

    8、const QPointF & QMouseEvent::screenPos() const

    Returns the position of the mouse cursor as a QPointF, relative to the screen that received the event.

    和QPoint QMouseEvent::globalPos() 值相同,但是类型更高精度的QPointF

    This function was introduced in Qt 5.0.

     

    总结一:经过试验,QMouseEvent::globalPos()  和 QCursor::pos()效果一样,但是Qt帮助文档说不一样,可是我获得值确实相同的。

    QCursor::pos() == QMouseEvent::globalPos() 都是全局坐标;

    总结二:将button:posBtn直接转换成全局坐标。

    QMouseEvent::globalPos() ==  ui.posBtn->mapToGlobal(ui.posBtn->pos());

    总结三:将全局坐标(鼠标当前坐标,QCursor::pos())直接转换成当前

    当前窗口相对坐标 ==  ui.posBtn->mapFromGlobal(QCursor::pos());

    如下图:

    0d729944ebf81a4cd9777d3fd52a6059242da644_2

    上面的mouseEvent.globalPos()和QCursor::pos()永远相同,都是全局坐标。

    上面绿色按钮的当前坐标:ui.pushButton->pos() 、转换父窗口坐标后mapToParent()、转换成全局坐标后mapToGlobal();

    如果当前鼠标坐标摸到按钮,按钮上面的文字发生变化,经过比较。

    确实得到:QCursor::pos() == ui.posBtn->mapFromGlobal(QCursor::pos());

     

    转载地址:http://jingyan.baidu.com/article/48206aeaffc02c216ad6b31d.html

  • 相关阅读:
    第二十三讲:访问者模式
    第二十二讲:备忘录模式
    第二十讲:迭代模式
    第十九讲:职责链模式
    第十八讲:中介者模式
    UTC时间、GMT时间、本地时间、Unix时间戳
    【基础】SQL Server系统库的作用
    【基础】SQL Server系统库的作用
    【基础】SQL Server系统库的作用
    【收集】C#一些基础的面试题
  • 原文地址:https://www.cnblogs.com/cthu/p/5112567.html
Copyright © 2020-2023  润新知