我正在将我的图形引擎从Freeglut移到Qt。 我的窗口类继承自QWindow。 我在将相对鼠标位置设置到窗口中心并隐藏光标时遇到问题。 在freeglut中,代码如下所示:
glutWarpPointer((glutGet(GLUT_WINDOW_WIDTH) / 2), (glutGet(GLUT_WINDOW_HEIGHT) / 2)); glutSetCursor(GLUT_CURSOR_NONE);我正在尝试这样的事情:
this->cursor().setPos((width() / 2), (height() / 2)); // this seems to set an absolute (global) position this->cursor().setShape(Qt::BlankCursor); // doesn't work怎么实现呢?
I'm moving my graphics engine from Freeglut to Qt. My window class inherits from QWindow. I have a problem with setting relative mouse position to the center of the window and hiding a cursor. In freeglut the code looks like this:
glutWarpPointer((glutGet(GLUT_WINDOW_WIDTH) / 2), (glutGet(GLUT_WINDOW_HEIGHT) / 2)); glutSetCursor(GLUT_CURSOR_NONE);I was trying something like this:
this->cursor().setPos((width() / 2), (height() / 2)); // this seems to set an absolute (global) position this->cursor().setShape(Qt::BlankCursor); // doesn't workHow to achieve that ?
最满意答案
您的代码没有任何效果,因为您正在编辑临时副本。 看看签名: QCursor QWidget::cursor() const 。 游标对象由值返回。 要应用光标更改,您必须通过setCursor()将修改后的对象传回。 要从本地坐标映射到全局坐标,请使用mapToGlobal() :
QCursor c = cursor(); c.setPos(mapToGlobal(QPoint(width() / 2, height() / 2))); c.setShape(Qt::BlankCursor); setCursor(c);Your code doesn't have any effect, because you're editing a temporary copy. Look at at the signature: QCursor QWidget::cursor() const. The cursor object is returned by value. To apply cursor changes, you must pass the modified object back via setCursor(). To map from local to global coordinates, use mapToGlobal():
QCursor c = cursor(); c.setPos(mapToGlobal(QPoint(width() / 2, height() / 2))); c.setShape(Qt::BlankCursor); setCursor(c);更多推荐
发布评论