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. Move QWidget inside parent widget,but it delay
Forum Updated to NodeBB v4.3 + New Features

Move QWidget inside parent widget,but it delay

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 3 Posters 574 Views
  • 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.
  • Joe JohnsonJ Offline
    Joe JohnsonJ Offline
    Joe Johnson
    wrote on last edited by
    #1
    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)

    1 Reply Last reply
    0
    • JoeCFDJ Offline
      JoeCFDJ Offline
      JoeCFD
      wrote on last edited by
      #2

      It looks like a few moves can happen:
      first move: move(mapToParent(event->pos() - offset));
      More possible moves could follow. Why do not you make one single move by combining all conditions together?

      Joe JohnsonJ 1 Reply Last reply
      1
      • JoeCFDJ JoeCFD

        It looks like a few moves can happen:
        first move: move(mapToParent(event->pos() - offset));
        More possible moves could follow. Why do not you make one single move by combining all conditions together?

        Joe JohnsonJ Offline
        Joe JohnsonJ Offline
        Joe Johnson
        wrote on last edited by
        #3

        @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

        JonBJ 1 Reply Last reply
        0
        • Joe JohnsonJ Joe Johnson

          @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

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @Joe-Johnson
          No. You can't (necessarily) do it with else ifs 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 single move(_x, _y) at the end. Whether it will help I'm not at all sure, but it would avoid multiple move()s if that is a problem.

          Joe JohnsonJ 1 Reply Last reply
          0
          • JonBJ JonB

            @Joe-Johnson
            No. You can't (necessarily) do it with else ifs 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 single move(_x, _y) at the end. Whether it will help I'm not at all sure, but it would avoid multiple move()s if that is a problem.

            Joe JohnsonJ Offline
            Joe JohnsonJ Offline
            Joe Johnson
            wrote on last edited by
            #5

            @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
            JonBJ JoeCFDJ 2 Replies Last reply
            0
            • Joe JohnsonJ Joe Johnson

              @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
              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #6

              @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 ifs, e.g. look at what it would do if both x() & y() are less than zero....

              1 Reply Last reply
              0
              • Joe JohnsonJ Joe Johnson

                @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
                JoeCFDJ Offline
                JoeCFDJ Offline
                JoeCFD
                wrote on last edited by
                #7

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

                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