c++ - Qt Mouse Control of Rotaries on touch and non-touch devices -


i have implemented rotary widget in qt. when user clicks rotary, mouse cursor hidden, , dragging mouse left/down turns rotary anti-clockwise, , dragging mouse right/up turns rotary clockwise. when mouse released, mouse cursor set original position of click. implemented this:

void rotarywidget::mousepressevent(qmouseevent *mouseevent) {     mmousepos = qcursor::pos();     mpreviouspos = mouseevent->pos();     setcursor(qt::blankcursor); }  void rotarywidget::mousereleaseevent(qmouseevent *mouseevent) {     qcursor::setpos(mmousepos);     unsetcursor(); }  void rotarywidget::mousemoveevent(qmouseevent *mouseevent) {     qpoint deltapos = mouseevent->pos() - mpreviouspos;     // use deltapos move rotary     mpreviouspos = mouseevent->pos(); } 

the benefit of when there several rotaries in row, quicker user make adjustments.

there bug above code, in if cursor gets edge of screen, mouse cannot moved rotary not move. however, user cannot see cursor, problem. changed mousemoveevent fix it:

void rotarywidget::mousemoveevent(qmouseevent *mouseevent) {     if(mouseevent->globalpos() == mmousepos)     {         mpreviouspos = mapfromglobal(mmousepos);         return;     }     else     {         qcursor::setpos(mmousepos);     }     qpoint deltapos = mouseevent->pos() - mpreviouspos;     // use deltapos move rotary     mpreviouspos = mouseevent->pos(); } 

essentially, every time mouse moved, change calculated, , cursor set original press position (so can never go off screen). if @ top makes sure don't continuously set mouse position original position on , over.

this works fine on non touch-screen devices, on touch screen devices, qcursor::setpos() not work because cannot programmatically set position of finger on screen!

i have failed think of solution satisfies of requirements have described. if there way detect whether user using touch screen fixed, can find no such function in qt.

instead of attempting force mouse click position, can check if mouse on edge or beyond edge, , if is, use constant increment delta (last move):

void rotarywidget::mousemoveevent(qmouseevent *mouseevent) {     qpoint deltapos;     if(!isinsidewindow(mouseevent))     {        deltapos = mprevdeltapos;     }     else     {        deltapos = mouseevent->pos() - mpreviouspos;        mprevdeltapos = deltapos;        // use deltapos move rotary        mpreviouspos = mouseevent->pos();      } } 

edit:

another option calculate deltapos base on global mouse coordinates rather local coordinates relative window 0 - since difference between 2 mouse coordinates of interest, position not need linked window. in case, widget work both touch , mouse operated screen intended - regardless of how cursor positioned respect window frame, same movements of mouse/finger change orientation in same way.


Popular posts from this blog

How to calculate SNR of signals in MATLAB? -

c# - Attempting to upload to FTP: System.Net.WebException: System error -

ios - UISlider customization: how to properly add shadow to custom knob image -