MouseMoveEvent() how to set the refresh interval?
-
Hi all,
I'm developing an application that makes a lot of operations at the same time.When the function mouseMoveEvent() of my principal widget is called, the application slows too while I move the mouse.
This application should not request a continuous refresh of the mouse position but the refresh can be done also 1 time on 10 so the slowdowns should disappear.
How can I do it?
-
I know of no way to throttle the interval.
Perhaps you can decouple the processing from the move event itself by using the move event to update an X/Y coordinate pair in your class, and then doing your data processing periodically via a QTimer. If you don't need to constantly redo the operations, additionally set a boolean flag to true when the coordinates are updated then have your timeout slot check to see if that flag has been set before attempting to do your processing on the most recent mouse coordinates (and then clear the flag.)
-
It's a good idea but how can I do to read the mouse current position only when I want?
-
You don't. You make your mouseMoveEvent very lightweight. And all it does is store the x and y position of the mouse in a couple of private variables. Then you can read those variables whenever you wish.
-
I think my mousemoveevent is already lightweight because the if condtions are rarely satisfied:
@void MatrixW::mouseMoveEvent(QMouseEvent *e)
{
if(thismove!=none)
{
int xnew=e->x()/larghcell;
int ynew =e->y()/altcell;if((xnew!=xN||ynew!=yN)&&(xnew<size_x && ynew<size_y)) { if (isarelease) { isarelease=false; if (thismove==del) setcell(xN,yN,0); else if (thismove==add) setcell(xN,yN,1); } xN=xnew; yN=ynew; if (thismove==del) setcell(xN,yN,0); else if (thismove==add) setcell(xN,yN,1); update(); } }
}@
(thismove!=none) only if the mouse is clicked
((xnew!=xN||ynew!=yN)&&(xnew<size_x && ynew<size_y)) is rarely true each 50-100 msThe good way to do this I think is to start a timer when the mouse was clicked, the timer call each 50-100ms a function like MatrixW::mouseMoveEvent(QMouseEvent *e) where the program can read the muose position.
The mouse release event stop the timer.But I don't know how to read the mouse position in the function called by the timer
continually read the mouse position would be the same as what I do now (about the slowdowns)