Move QWidget inside parent widget,but it delay
-
void ChildWidget::mousePressEvent(QMouseEvent *event) { offset = event->pos(); } void ChildWidget::mouseMoveEvent(QMouseEvent *event) { //Using left mouse to move the control if (event->buttons() & Qt::LeftButton) { //Excute movement follow mouse position move(mapToParent(event->pos() - offset)); //Make sure control do not move out parent size if (x() < 0) move(1, y()); if (y() < 0) move(x(), 1); if (x() + width() > parentWidget()->width()) move(parentWidget()->width() - 1 - width(), y()); if (y() + height() > parentWidget()->height()) move(x(), parentWidget()->height() - 1 - height()); } }
I have tried to make a child widget follow my mouse cursor and limit in into parent widget.It works,however,the child widget looks like to shrink when I move the child widget fast.According to Qt doc, the pos is the position of the last mouse move event.How can I make it looks "normal"?(no shrink)
-
@JoeCFD thanks for your reply.Do you mean the follow part?
//Make sure control do not move out parent size if (x() < 0) move(1, y()); else if (y() < 0) move(x(), 1); else if (x() + width() > parentWidget()->width()) move(parentWidget()->width() - 1 - width(), y()); else if (y() + height() > parentWidget()->height()) move(x(), parentWidget()->height() - 1 - height()); else move(mapToParent(event->pos() - offset));
however,it still looks to shrink
-
@Joe-Johnson
No. You can't (necessarily) do it withelse if
s anyway, as more than one condition might apply.@JoeCFD is suggesting you use a couple of variables, say
_x
&_y
. Do all your calculations first to set both of these. then do a singlemove(_x, _y)
at the end. Whether it will help I'm not at all sure, but it would avoid multiplemove()
s if that is a problem. -
@JonB thanks.```
QPoint point(mapToParent(event->pos() - offset));
//Make sure control do not move out parent size
if (x() < 0)
{
point.setX(1);
point.setY(y());
}
else if (y() < 0)
{
point.setX(x());
point.setY(1);
}
else if (x() + width() > parentWidget()->width())
{
point.setX(parentWidget()->width() - 1 - width());
point.setY(y());
}
else if (y() + height() > parentWidget()->height())
{
point.setX(x());
point.setY(parentWidget()->height() - 1 - height());
}
move(point);however,maybe the multiple move()s is not the reason of the shrinking child widget
-
@Joe-Johnson said in Move QWidget inside parent widget,but it delay:
however,maybe the multiple move()s is not the reason of the shrinking child widget
That is probably correct! So this may be wasting your time. But your code is still not great with
else if
s, e.g. look at what it would do if bothx()
&y()
are less than zero.... -
@Joe-Johnson add a print message in this func to check how many moves are done. This may not be the root cause. But I would clean this up first if I were you.