2

I'm writing a project in Qt Creator, and if I write

QPoint cursorPos=QCursor::pos();

then cursorPos={-2147483648,-2147483648} which is obviously wrong. However, if I write

QApplication *application=new QApplication(argc,argv);
QPoint cursorPos=QCursor::pos();

then cursorPos is the correct mouse position. Is there any way I can get QCursor::pos() to work without QApplication? Thanks. :D

user3551745
  • 33
  • 2
  • 9

1 Answers1

4

QApplication object does so much initialization. One of the QApplication's main areas of responsibility in the Qt documentation :

It manages the application's mouse cursor handling, see setOverrideCursor()

Also from the Qt documentation about QCursor :

Note: It is possible to create a QCursor before QGuiApplication, but it is not useful except as a place-holder for a real QCursor created after QGuiApplication. Attempting to use a QCursor that was created before QGuiApplication will result in a crash.

So it seems that it is not possible to use QCursor without QApplication or QGuiApplication.

Nejat
  • 31,784
  • 12
  • 106
  • 138