Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Strange behavior of mouse events or QCursor::setPos() does not work
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 499 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • mrjbomM Offline
    mrjbomM Offline
    mrjbom
    wrote on last edited by mrjbom
    #1

    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?

    1 Reply Last reply
    0
    • Kent-DorfmanK Offline
      Kent-DorfmanK Offline
      Kent-Dorfman
      wrote on last edited by
      #2

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

      I light my way forward with the fires of all the bridges I've burned behind me.

      mrjbomM 1 Reply Last reply
      0
      • Kent-DorfmanK Kent-Dorfman

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

        mrjbomM Offline
        mrjbomM Offline
        mrjbom
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved