PDA

View Full Version : moving labels to exact location with mouse move event resulted in jumpy locations



faizol
9th October 2012, 21:47
Hi all,

I've searched and read several documents and threads regarding exact positioning of a child widget. I tried to code it but still having a problem.

Here's my code;



void BoxExample::mouseMoveEvent(QMouseEvent *event)
{
if(_mouseLeftPressed && this->objectName()=="Box_A"){ // Box_A type QLabel

this->move(mapToParent(event->pos()-_offset)); // Box_A moves as expected

_tmp=this->parentWidget()->findChild<QWidget *>("Box_B"); // Box_B type QLabel
if(_tmp){
_tmp->move(mapToParent( QPoint(0, event->pos().y()-_offset.y()) ) );// Box_B jumps around
_tmp=NULL;
}

}
}


I can get Box_A to move exactly where I wanted it to be, but Box_B which is supposed to be sliding up and down the Y axis is jumping around in the parent widget. What's the right method for the child coordinate transformation?


------------------------------------------------------------------------

I solved it by passing this->pos().y() position directly to Box_B.



void BoxExample::mouseMoveEvent(QMouseEvent *event)
{
if(_mouseLeftPressed && this->objectName()=="Box_A"){ // Box_A type QLabel

this->move(mapToParent(event->pos()-_offset)); // Box_A moves as expected

_tmp=this->parentWidget()->findChild<QWidget *>("Box_B"); // Box_B type QLabel
if(_tmp){
_tmp->move(QPoint(0, this->pos().y() ));// Box_B slides up and down Y axis
_tmp=NULL;
}

}
}


Still do not understand the whole mechanism behind child parent transform properly. It seems that all transformations are done relatively, right?


Thanks.

Cheers,
Faizol