- 积分
- 12
贡献77
飞刀19 FD
注册时间2019-10-18
在线时间2 小时
|
楼主 |
发表于 2023-8-14 09:08:59
|
显示全部楼层
附件为源码,
源码只写了一个CPP的类,调用了QCursor::pos();方法获取鼠标坐标
cursortest.cpp
- #include "cursortest.h"
- #include <QCursor>
- #include <QPoint>
- #include <QtDebug>
- CursorTest::CursorTest(QObject *parent) : QObject(parent)
- {
- }
- void CursorTest::getCurPos()
- {
- QPoint tempP=QCursor::pos();
- qDebug()<<"CPP_QCursor x: "+QString::number(tempP.x())+" y: "+QString::number(tempP.y());
- }
复制代码 main.qml中调用了getCurPos方法
- import QtQuick 2.11
- import QtQuick.Window 2.11
- import MyTest 1.0
- Window {
- visible: true
- width: 640
- height: 480
- title: qsTr("Hello World")
- //flags: Qt.FramelessWindowHint
- MouseArea{
- id:mainArea
- anchors.fill: parent
- focus:true
- onPressed : {
- print("QML MouseArea_x:",mouse.x,",y:",mouse.y);
- myTest.getCurPos();
- }
- }
- CursorTest{
- id:myTest
- }
- }
复制代码 点击屏幕,控制台会打出日志。一个是通过MouseArea获取的鼠标位置,结果正常。一个是通过QCursor::pos();打出的鼠标屏幕的位置,则全为0.日志如下:
- qml: QML MouseArea_x: 374.6015625 ,y: 258.75
- "CPP_QCursor x: 0 y: 0"
- qml: QML MouseArea_x: 365 ,y: 243.125
- "CPP_QCursor x: 0 y: 0"
- qml: QML MouseArea_x: 373 ,y: 238.75
- "CPP_QCursor x: 0 y: 0"
复制代码
|
|