PDA

View Full Version : Cant move my widget properly



tonnot
19th July 2011, 10:13
I have a simple code (i think) to move a simple Widget placed at the central-widget of a window. I have subclass the mouseevents to do the work but it does not work properly.
( the widget flikks and on every movement of 1 pixel the mouse is 1 pixel from it have to be placed... )

Mousemove:

int mouse_x = event->pos().x();
int mouse_y = event->pos().y();
if (mouse_active==Qt::LeftButton) {
mouse_prev= mouse_active;
mouse_active=Qt::NoButton; // to avoid re-call
event->accept();
// mouse_rx, mouse_ry are private vars . First value is given by mousepress, the nexts from here
this->move(this->geometry().left()+mouse_x-mouse_rx,this->geometry().top() +mouse_y-mouse_ry);
mouse_rx=mouse_x;mouse_ry=mouse_y; // remember
mouse_active=mouse_prev; // restore the button type}

Mousepress :

mouse_active = event->button();
mouse_rx = event->pos().x();
mouse_ry = event->pos().y();


Any idea ? (I'm tired to see how so simple thing does not work ....)

mvuori
19th July 2011, 19:36
The code is somewhat hard to follow, due to the varying indentation (nowadays when Python is familiar to many, they are prone to read too much into any indentation) and the naming of variables. mouse_x and mouse_rx do not eplain much on their purpose and get confused in the tightly typed code.

But still, the way I see it, the problem is the approach where you move the widget based on the delta of last two mouse moves. But the widget may lag behind.
I would suggest an approach where you in the click calculate the distance on the clik from the widget. Something like this:
* in mousePressEvent:
QPoint positionDiffOfMouseAndWidget = event->pos() - this->geometry().topLeft();
* in mouseMoveEvent:
QPoint newpoint = event->pos() - positionDiffOfMouseAndWidget;
this->move(newpoint);

tonnot
19th July 2011, 20:07
Thanks, I'm going to try it.