简体   繁体   中英

How to set mouse cursor position in QML

I am using a QQuickFrameBufferObject to render 3D objects in a QML app. From the app, I would like for the mouse cursor to warp on the QQuickFrameBufferObject surface when I am rotating or translating the objects displayed.

Rotation and translation works great, but I can't find a way to control the mouse position.

Edited for better explanation on what I meant by warping : What I mean by warping the mouse cursor is that when you are rotating a 3D object on the screen, you click & drag the mouse. But when you reach the end of the screen on the right, you can no longer drag to the right and you have to release the mouse button, move your mouse to the left, click & drag again to continue your rotation.

What I want to do is teleport (or warp ) the mouse cursor to the left hand side of my rendering surface when I am dragging and I reach the right hand side of the rendering surface, allowing for infinite movement when I rotate

Is there a way for me to achieve this effect (could be simply by setting the position from a JS script or from the C++ backend when the mouse reaches the border or by having a way to grab the mouse and retrieve the relative positions as can be done in some window libraries).

I don't exactly understand your needs, but maybe the QCursor -class helps you.

You can integrate it eg like this:

#include <QObject>
#include <QCursor>

class MyObj : public QObject
{
    Q_OBJECT
public:
    explicit MyObj(QObject *parent = 0) : QObject(parent)
    {

    }

signals:

public slots:
    void moveCursor() { cursor.setPos(0, 0); } // Sets it to the top left corner of the screen

private:
    QCursor cursor;
};

register it to QML and use it there:

ApplicationWindow {
    id: rootWin
    width: 800; height: 600; visible: true

    MyObj {
        id: mob

    }

    Button {
        anchors.centerIn: parent
        onClicked: mob.moveCursor()
    }
}

So from what I understand what is missing is, how to calculate the position to which you want to move the mouse (they are screen not windows coordinates, so mapToGlobal ) and how to trigger it, but that you know best, I guess.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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