Qt World Summit: Submit your Presentation

How to constrain window movement?

  • Hi,

    Is there a way to constrain window movement so that a user cannot drag a window on one axis (e.g. horizontal)? I have tried implementing using widget move events but they only get fired when the user stops the drag action on a window. I would however like to get events already while the window is moving. The idea is to prevent the window from ever overlapping a specific area of the screen, even temporarly.

    Thanks for any help.

  • Hi,

    you should enable mouseTracking, in order to receive mouse event "while moving".

    "Set Mouse Tracking":http://doc.qt.nokia.com/4.7/qwidget.html#mouseTracking-prop


  • Well, thinking better about your post, I guess that moveEvent is not useful in your case.

    So you should try to catch the click and follow the mouse movement correcting the geometry along the drag. That's an idea, I've never experienced it.


  • Good idea but there are no mouseMoveEvents on a window while it is being dragged.

  • what about QWidget::dragMoveEvent(QDragMoveEvent *event)?

  • you can use setFixedHeight( int ) in this way your widget can't expand on vertically and will have a fixed height, if you want that your widget to be fixed over a certain height you can use setMaximumHeight

  • Thanks for the tips.

    dragMoveEvent() is meant for drag&drop, e.g. when dragging a file from one window to another. It is not for dragging windows itself.

    The widget height is not the issue for me, it is its movement, so fixing the height wouldn't help much.

    I am now looking at putting the widget into an QMdiArea to see if I can get the desired effect this way.

  • bq.
    dragMoveEvent() is meant for drag&drop, e.g. when dragging a file from one window to another. It is not for dragging windows itself.

    dragMoveEvent() can be used while dragging widgets too. Have you seen the Draggable Icons demo that ships with Qt?

  • Maybe I should have been more explicit. The widget is a window which is either being moved by dragging the window frame handle (i.e. the window decoration) or by pressing Alt+LMB and dragging the window (this application is running under Gnome).
    I just tested it but for neither of the two cases I get any dragMoveEvents, not on the window itself nor on the widget I am dragging it across.

  • Thanks for the pointer.

    In my particular case I solved it using an MDI area and intercepting mouse events on it through eventFilter(). That way I was able to rewrite the events to maintain the same X value before they are sent to the actual movement handler.

  • glad to hear that.
    could you also create a new wiki page and add your code snip? will help other developers :)

  • I'd like to but my employer would not like it at all, even if it was abstract code, so unfortunately I can't say more than I have.

Log in to reply