c++ - 如何在不使用 QApplication 的情况下使用 QCursor::pos() ?

标签 c++ c qt qt-creator qapplication

我正在 Qt Creator 中编写一个项目,如果我编写

QPoint cursorPos=QCursor::pos();

然后cursorPos={-2147483648,-2147483648}这显然是错误的。但是,如果我写

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

那么cursorPos就是正确的鼠标位置。有什么方法可以让 QCursor::pos() 在没有 QApplication 的情况下工作吗?谢谢。 :D

最佳答案

QApplication 对象做了很多初始化工作。 Qt 文档中 QApplication 的主要职责领域之一:

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

还来自有关 QCursor 的 Qt 文档:

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.

因此,在没有 QApplicationQGuiApplication 的情况下,似乎无法使用 QCursor

关于c++ - 如何在不使用 QApplication 的情况下使用 QCursor::pos() ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24582682/

相关文章:

c++ - 什么是 undefined reference /未解析的外部符号错误,我该如何解决?

C++ 虚方法引发 EXC_BAD_ACCESS(仅当由 Objective-C++ 调用时)

c++ - 使用configure文件生成makefile

c - pthread_create 并传递一个整数作为最后一个参数

c - 如何正确测量CUDA时间?

c++ - QByteArray 的标准替代品

python - 如何检测 PySide Gui 上的任何鼠标点击?

c++ - 无法调用其他类 C++ 的析构函数

c - 使用 ANSI C 实现工厂模式

C++/Qt Q_OBJECT宏导致错误