Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Making movable QRubberBand be only movable within parent widget
Qt 6.12 LTS is out! See what's new in the release blog

Making movable QRubberBand be only movable within parent widget

Scheduled Pinned Locked Moved General and Desktop
1 Posts 1 Posters 2.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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    Hello,

    I have a rectangular QRubberBand that its size is made with the mouse clicking and dragging. Once the mouse is released the rectangle stays on screen and if you click and drag the rectangle you begin moving it. It all works fine except that I am trying to restrict the movement to only be within parent widget boundaries, because it can move out of the screen right now.

    Is there something similar to QGraphicsItem::itemChange(..) for a QWidget based widget? I think there I could maintain the rectangle within bounds every time it gets out with the ItemPositionChange flag.. I am looking for a similar way of handling this case witha QRubberband.

    So far tried in the mouseMoveEvent of my parent widget to do :

    @
    if(viewport()->geometry().contains(m_rubberBand->geometry(), true))
    moveRectangle(event);
    else
    {
    //just testing if I move the rectangle of screen completely to the left where x < 0
    QPoint newPos = m_rubberBand->geometry().topLeft();
    QRect viewRect = viewport()->geometry();

        newPos.setX(qMin(viewRect.right(), qMax(newPos.x(), viewRect.left())));
        // w and h are the current width and height which I have once a rectangle is created
        m_rubberBand->setGeometry(newPos.x(), newPos.y(), w, h);
    

    }
    @

    What this does so far it avoids moving the rectangle out of the parent widget bounds but it gets stuck to the edge and its not possible to move anymore. Any help greatly appreciated!

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