Important: Please read the Qt Code of Conduct -

Moving the cursor

  • So say I open a new window. I want the mouse cursor to move to a random position on the window that was spawned ( the location doesn't really matter ).

    Is it possible?

    I tried using QCursor's setPos function and I had the position of the widget that was spawned withthis->pos();
    but it didn't work

  • Lifetime Qt Champion


    How exactly did you implement that ?

    On a side note, you should really think about that idea. People usually don't find it nice that their cursor starts moving around without them asking for some sort of assistance for that.

  • @SGaist

    I'm implementing it like this:

        def move_cursor(self, x, y):
            screen = QGuiApplication.primaryScreen()
            cursor = QCursor()
            cursor.setPos(x, y)

    If I replace cursor.setPos(x, y) with cursor.setPos(screen, x, y) then it errors and says:

    TypeError: arguments did not match any overloaded call:
     setPos(int, int): argument 1 has unexpected type 'QScreen'
     setPos(QPoint): argument 1 has unexpected type 'QScreen'```

  • Lifetime Qt Champion

    setPos is a static method.

    By the way, what version of Qt are you using ?

  • @SGaist

    When I tested it in pycharm it didn't work but after running the script from a terminal it works but the cursor gets put in a wrong position. I will find a fix for that later but thanks for trying to help!

Log in to reply