[Solved]How to move a widget along the Y axis keeping x position as constant.
-
[quote author="utcenter" date="1348574366"]Move widget to widget's X and event's Y
@move(x(), event->pos().y());@ [/quote]
Nops It creates flickering effect and does not move as required , how ever i tried
@move(mapToParent(QPoint(0,event->pos().y())));@
it works, but the y position is not proper.
-
I think you are doing it a bit backwards. You try to move things from the inside, in which case you need to map to the parent coordinates. The simpler solution is to have the parent move the movable widget in its own coordinate system, which is only:
@void Widget::mouseMoveEvent(QMouseEvent *event) {
movable->move(movable->x(), event->pos().y());
}@You can select which widget to move by assigning its address to the movable pointer.
-
So now I implemented the same by overriding the mousePressEvent , mouseMoveEvent and mouseReleaseEvent of the parent class.
@void Widget::mousePressEvent(QMouseEvent *event)
{
widget = static_cast<CustomWidget *>(childAt(event->pos()));
if (!widget)
return;y = event->pos().y() - widget->pos().y();
}
void Widget::mouseMoveEvent(QMouseEvent *event)
{
if (!widget)
return;widget->move(widget->x(),event->pos().y()-y);
}@
This works perfect.
Thanks for the help :)
-
Dear Andre,
The "closed thread":http://qt-project.org/forums/viewthread/20762/ and the current thread may have the same question but the implementation requirements are different. For the current topic I need to move the widget along Y-axis keeping x- constant. But for the other one I am implementing drag and drop where I need to move the QDrag::pixmap. I am trying to use QDrag::setHotSpot() in the dragMoveEvent() but I am unable to achieve the same.
Thanks