[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

    !http://imageshack.us/a/img10/7092/capturejzft.png(move)!

    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)
    {
    move(mapToParent(QPoint(pos().x(),event->pos().y())));
    }@

    but it shifts the x positon like

    !http://imageshack.us/a/img211/1845/capturetmf.png(shifted)!

    and if it try

    @move(mapTo(this->parentWidget(),QPoint(event->pos().x(),event->pos().y())));@

    then it shiftes the image to the mouse position like :
    !http://imageshack.us/a/img140/3056/capturebqg.png(mouse position)!

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



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



  • Something like : ???

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



  • 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

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

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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.