Home > other >  How to get global mouse position in Qt6?
How to get global mouse position in Qt6?

Time:08-22

I'm trying to get global position of the mouse however the functions mentioned in older topics are either deprecated or not working as intended. I have tried QCursor::pos() as seen below but it didn't work as intended.

#include <QtCore/QCoreApplication>
#include <QCursor>
#include <iostream>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    while(1)
    {
        std::cout << QCursor::pos().x() << " " << QCursor::pos().y() << std::endl;
    }

    return a.exec();
}

Output: 2147483647 2147483647

CodePudding user response:

It is very simple. QCursor is in the Gui module of Qt see here, so you have to change QCoreApplication to QGuiApplication and then it works (already tested it).

  • Related