Kitiara
Kitiara

Reputation: 498

How to get global mouse position in Qt6?

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

Upvotes: 1

Views: 436

Answers (1)

Tim
Tim

Reputation: 459

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).

Upvotes: 1

Related Questions