Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved]How to move a widget along the Y axis keeping x position as constant.
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved General and Desktop
13 Posts 3 Posters 13.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Sam
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on last edited by
      #2

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

      1 Reply Last reply
      0
      • S Offline
        S Offline
        Sam
        wrote on last edited by
        #3

        Something like : ???

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

        1 Reply Last reply
        0
        • U Offline
          U Offline
          utcenter
          wrote on last edited by
          #4

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

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

          1 Reply Last reply
          0
          • S Offline
            S Offline
            Sam
            wrote on last edited by
            #5

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

            1 Reply Last reply
            0
            • U Offline
              U Offline
              utcenter
              wrote on last edited by
              #6

              Who intercepts the event, the widget or the parent?

              1 Reply Last reply
              0
              • S Offline
                S Offline
                Sam
                wrote on last edited by
                #7

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

                The widget intercepts the events.

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  Sam
                  wrote on last edited by
                  #8

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

                  1 Reply Last reply
                  0
                  • U Offline
                    U Offline
                    utcenter
                    wrote on last edited by
                    #9

                    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.

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      Sam
                      wrote on last edited by
                      #10

                      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.

                      1 Reply Last reply
                      0
                      • U Offline
                        U Offline
                        utcenter
                        wrote on last edited by
                        #11

                        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.

                        1 Reply Last reply
                        0
                        • S Offline
                          S Offline
                          Sam
                          wrote on last edited by
                          #12

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

                          1 Reply Last reply
                          0
                          • S Offline
                            S Offline
                            Sam
                            wrote on last edited by
                            #13

                            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

                            1 Reply Last reply
                            0

                            • Login

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • Users
                            • Groups
                            • Search
                            • Get Qt Extensions
                            • Unsolved