Important: Please read the Qt Code of Conduct -

[Solved]How to move a widget along the Y axis keeping x position as constant.

  • I have a form that has few CustomWidget added to it using VBoxLyaout. eg


    Here the above colored rectangles represent the custom widget. I would like to move the widgets up or down keeping the x position as constant. In order to do that i an handling mousePressEvent, mouseMoveEvent on my customwidgets

    @void CustomWidget::mouseMoveEvent(QMouseEvent *event)

    but it shifts the x positon like


    and if it try


    then it shiftes the image to the mouse position like :
    ! position)!

    How to move it along Y axis keeping x as constant?

  • How about simply not changing the x coordinate, but keeping it stable?

  • Something like : ???


  • Move widget to widget's X and event's Y

    @move(x(), event->pos().y());@

  • [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


    it works, but the y position is not proper.

  • Who intercepts the event, the widget or the parent?

  • [quote author="utcenter" date="1348575090"]Who intercepts the event, the widget or the parent?[/quote]

    The widget intercepts the events.

  • Ok my bad , Got the answer,

    in the mousePressEvent() i got the y() location using

    @y = event()->pos().y();@

    Then in the mouseMoveEvent() i used

    @move(mapToParent(QPoint(0,(event->pos().y() - y))));@

    Thanks for the help :)

  • 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.

  • I just figured out the same. i need to implement it outside , like in the parent widget. Changing the code now.

    Thanks for pointing this out.

  • Well, it is not mandatory, but it is easier, however not always possible, sometimes you might get into a situation where you need to do it the other way or it is easier. That is why those mapToParent methods exist.

  • 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)

    y = event->pos().y() - widget->pos().y();


    void Widget::mouseMoveEvent(QMouseEvent *event)
    if (!widget)



    This works perfect.

    Thanks for the help :)

  • Dear Andre,

    The "closed thread": 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.


Log in to reply