Important: Please read the Qt Code of Conduct -

QCursor::pos() is not screen coordinates

  • Recently switched to Fedora 32 x86-64 with QT5.13.2, and, now, QCursor::pos() is not returning screen coordinates. Instead, it has the following behaviour:

    1. When inside a QT application window of the application that calls QCursor::pos(), the returned coordinates are relative to the application window.
    2. When outside a QT application window, the returned coordinates are constant -- whatever was last returned when it was inside a QT application window.

    The doc says, The position is queried from the windowing system, so I suppose I need to set some flag somewhere??

    My distro does not supply a debuginfo version of the qt5 libraries, but I did check the source code for QCursor::pos():

    QPoint QCursor::pos(const QScreen *screen)
        if (screen) {
            if (const QPlatformCursor *cursor = screen->handle()->cursor()) {
                const QPlatformScreen *ps = screen->handle();
                QPoint nativePos = cursor->pos();
                ps = ps->screenForPosition(nativePos);
                return QHighDpi::fromNativePixels(nativePos, ps->screen());
        return QGuiApplicationPrivate::lastCursorPosition.toPoint();

    The last line would explain why I get repeated values outside an application window, but why would it need to execute that return statement, rather than the one in the if statement?

  • In linux every script-kiddie run project wants control of your desktop, because "Their desktop is the best". Unforuntately this has led to many abominations with regard of getting real information out of the windowing system...and this wayland crap is just going to make it worse. My advice is to ditch anything having to do with gnome and use a simple X11 window manager like fvwm or twm.

  • Lifetime Qt Champion

    @normvcr Can you try with a more recent Qt version? Also, can you try with Qt provided by your Linux distribution?

  • @jsulm This is the QT of my distro installer

  • @Kent-Dorfman That was exactly the problem. QCursor::pos() works correctly under GNOME classic and GNOME xorg, but not under default GNOME. Many thinks.

Log in to reply