嵌入式爱好者

查看: 1534|回复: 2

[Linux] OKMX8MPQ无法通过QCursor::pos()获取鼠标位置

[复制链接]

1

主题

2

帖子

12

积分

扫一扫,手机访问本帖
发表于 2023-8-9 10:30:42 | 显示全部楼层 |阅读模式
直接调用QCursor::pos()
  1. void CursorTest::getCurPos()
  2. {
  3.     QPoint tempP=QCursor::pos();
  4.     qDebug()<<"CPP_QCursor x: "+QString::number(tempP.x())+" y: "+QString::number(tempP.y());
  5. }
复制代码
在linux虚拟机中没有问题。但交叉编译运行在板子上是,获取值始终是0,0。请问这是问什么。和weston有关吗?

左图为linux虚拟机下运行结果,右图为板子运行结果

回复

使用道具 举报

0

主题

29

帖子

93

积分

i.MX8MM通行证i.MX8MP通行证

发表于 2023-8-10 11:02:50 | 显示全部楼层
我们对qt也不是很了解,您把源码发过来,我们帮您看一下?

点评

附件为源码, 源码只写了一个CPP的类,调用了QCursor::pos();方法获取鼠标坐标 cursortest.cpp main.qml中调用了getCurPos方法 点击屏幕,控制台会打出日志。一个是通过MouseArea获取的鼠标位置,结果正常。一  详情 回复 发表于 2023-8-14 09:08
点评回复 支持 反对

使用道具 举报

1

主题

2

帖子

12

积分

 楼主| 发表于 2023-8-14 09:08:59 | 显示全部楼层
w_y_y 发表于 2023-8-10 11:02
我们对qt也不是很了解,您把源码发过来,我们帮您看一下?

附件为源码,
源码只写了一个CPP的类,调用了QCursor::pos();方法获取鼠标坐标

cursortest.cpp
  1. #include "cursortest.h"
  2. #include <QCursor>
  3. #include <QPoint>
  4. #include <QtDebug>

  5. CursorTest::CursorTest(QObject *parent) : QObject(parent)
  6. {

  7. }

  8. void CursorTest::getCurPos()
  9. {
  10.     QPoint tempP=QCursor::pos();
  11.     qDebug()<<"CPP_QCursor x: "+QString::number(tempP.x())+" y: "+QString::number(tempP.y());
  12. }
复制代码
main.qml中调用了getCurPos方法
  1. import QtQuick 2.11
  2. import QtQuick.Window 2.11
  3. import MyTest 1.0

  4. Window {
  5.     visible: true
  6.     width: 640
  7.     height: 480
  8.     title: qsTr("Hello World")
  9.     //flags: Qt.FramelessWindowHint

  10.     MouseArea{
  11.         id:mainArea
  12.         anchors.fill: parent
  13.         focus:true
  14.         onPressed : {
  15.             print("QML MouseArea_x:",mouse.x,",y:",mouse.y);
  16.             myTest.getCurPos();
  17.         }

  18.     }
  19.     CursorTest{
  20.         id:myTest
  21.     }
  22. }
复制代码
点击屏幕,控制台会打出日志。一个是通过MouseArea获取的鼠标位置,结果正常。一个是通过QCursor::pos();打出的鼠标屏幕的位置,则全为0.日志如下:
  1. qml: QML MouseArea_x: 374.6015625 ,y: 258.75
  2. "CPP_QCursor x: 0 y: 0"
  3. qml: QML MouseArea_x: 365 ,y: 243.125
  4. "CPP_QCursor x: 0 y: 0"
  5. qml: QML MouseArea_x: 373 ,y: 238.75
  6. "CPP_QCursor x: 0 y: 0"
复制代码



MousePosTest.tar

60 KB, 下载次数: 1, 下载积分: 贡献 1

源码

点评回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋| 飞凌嵌入式 ( 冀ICP备12004394号-1 )

GMT+8, 2024-5-7 21:53

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表