Please nominate your Qt Champions for 2021! https://forum.qt.io/topic/132134/looking-for-the-2021-qt-champions

Strange behavior of mouse events or QCursor::setPos() does not work



  • I am trying to lock the mouse in the widget, for this I constantly set the mouse position in the center of the widget, and to move the camera in my scene I use the virtual cursor position.

    void MainOpenGLWidget::mousePressEvent(QMouseEvent* event)
    {
        virtualMousePosition = this->rect().center();
        qInfo() << "press event:";
        qInfo() << "real mouse position:" << event->pos();
        qInfo() << "virtual mouse position:" << virtualMousePosition;
        setCursor(Qt::BlankCursor);
        QCursor::setPos(mapToGlobal(this->rect().center()));
        QMouseEvent eventCopy(event->type(), virtualMousePosition, event->button(), event->buttons(), event->modifiers());
        sceneManager->callMousePressEventHandler(eventCopy);
        QWidget::mousePressEvent(event);
    }
    
    void MainOpenGLWidget::mouseMoveEvent(QMouseEvent* event)
    {
        //"Note that the returned value(event::button()) is always Qt::NoButton for mouse move events."
    
        if (event->buttons() & Qt::LeftButton && event->pos() != rect().center())
        {
            virtualMousePosition += (event->pos() - rect().center());
            qInfo() << "move event:";
            qInfo() << "real mouse position:" << event->pos();
            qInfo() << "virtual mouse position:" << virtualMousePosition;
            QMouseEvent eventCopy(event->type(), virtualMousePosition, event->button(), event->buttons(), event->modifiers());
            sceneManager->callMouseMoveEventHandler(eventCopy);
            QCursor::setPos(mapToGlobal(this->rect().center()));
        }
        QWidget::mouseMoveEvent(event);
    }
    

    So, what's the problem I'm facing?
    I have two cases:

    1. I click a button on the widget(I don't move the mouse at the time of clicking) and then start moving the mouse left and this is the output I get:
    press event:
    real mouse position: QPoint(504,308)
    virtual mouse position: QPoint(359,340)
    move event:
    real mouse position: QPoint(358,340) <- All is fine, the mouse is in the center of the widget
    virtual mouse position: QPoint(358,340)
    move event:
    real mouse position: QPoint(357,340)
    virtual mouse position: QPoint(356,340)
    move event:
    real mouse position: QPoint(358,340)
    virtual mouse position: QPoint(355,340)
    move event:
    real mouse position: QPoint(358,340)
    virtual mouse position: QPoint(354,340)
    move event:
    real mouse position: QPoint(356,340)
    virtual mouse position: QPoint(351,340)
    
    1. I move the mouse to the left over the widget and press the mouse button while moving the mouse.
      Here's the output I get:
    press event:
    real mouse position: QPoint(638,309)
    virtual mouse position: QPoint(359,340)
    move event:
    real mouse position: QPoint(637,309) <- Something went wrong, my mouse cursor didn't move to the center of the widget!
    virtual mouse position: QPoint(637,309)
    move event:
    real mouse position: QPoint(634,309)
    virtual mouse position: QPoint(912,278)
    move event:
    real mouse position: QPoint(356,340)
    virtual mouse position: QPoint(909,278)
    move event:
    real mouse position: QPoint(358,340)
    virtual mouse position: QPoint(908,278)
    move event:
    real mouse position: QPoint(355,340)
    virtual mouse position: QPoint(904,278)
    move event:
    real mouse position: QPoint(357,340)
    virtual mouse position: QPoint(902,278)
    move event:
    real mouse position: QPoint(358,340)
    virtual mouse position: QPoint(901,278)
    move event:
    real mouse position: QPoint(356,340)
    virtual mouse position: QPoint(898,278)
    

    It turns out that if I press the button while moving the mouse, the cursor is not placed in the center and I have problems.
    How can I avoid this?



  • do you really want to pass the event to the parent after processing it in the override?



  • @Kent-Dorfman Even if I don't do it, I will face this problem.


Log in to reply