Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Stack overflow when using MouseMoveEvent



  • Hi,

    So i tried to implement a scene with a moving view with the view moving when the mouse is near one of the borders of the screen (like in most RTS games). However i get a stack overflow when i do that.
    My function looks like this

    void Camera::mouseMoveEvent(QGraphicsSceneMouseEvent* e)
    {
    //view coordinates
    int local_x = view->mapFromScene(e->pos()).x();
    int local_y = view->mapFromScene(e->pos()).y();

    int horizontalSliderPos = view->horizontalScrollBar()->sliderPosition();
    int verticalSliderPos = view->verticalScrollBar()->sliderPosition();
    if (local_x < 100) {
    	view->horizontalScrollBar()->setSliderPosition(horizontalSliderPos -5);
    }
    

    This code is only for the one direction to make it easier. Could someone please enlighten me where or how an overflow is occuring here?


  • Lifetime Qt Champion

    Hi,

    Aren't you triggering an infinite recursion ?



  • I mean probably. Thats where the stack overflow should come from i guess. But my question was where this occurs oe rather how can i fix that?

    Edit: From my (limited) understanding it should come from the fact that i trigger this event infinite times once the mouse is in the stated area. A fix for this would be to stop the event from happening once the slider is at its max/min position, but i have no idea how i would do that.


  • Lifetime Qt Champion

    Rather than relying on the slider position, shouldn't you rather define zones in which if the cursor can be found, the scrollbars are updated ?



  • Isnt that what im doing? I only update the slider position if the cursor (local_x/y) is at a specific location (< 100 in this case for x).


  • Lifetime Qt Champion

    Something is not clear from your code, what class is Camera ?



  • It inherits from QGraphicsRectItem and does not much else right now. It is added to the scene in the main class and then it grabs the mouse as otherwise the mouseMoveevent does not work.


  • Qt Champions 2019

    @infestor said in Stack overflow when using MouseMoveEvent:

    Thats where the stack overflow should come from i guess

    Simply looking at the backtrace will show it, it will also show you why it's a recursion.


Log in to reply