Qt: window with movable widgets

3.8k views Asked by At

I want to create a window with a variable number of custom widgets, each of the widgets can be a group of some simple objects. Widgets should behave similar to dialog windows: they can be moved anywhere inside the parent window, can overlap etc. However, they can't be moved out of the window. What would be the most appropriate way to implement something like this with QT3? I tried creating windows/dialogs with the main window as parent, but this way the widgets can be moved out of the window.

1

There are 1 answers

0
Liz On BEST ANSWER

You might be able to make use of the drag-and-drop functionality. That's the only thing I can think of.

There is an example that comes with Qt called Fridge Magnets. Basically they created a class called DragWidget which is the host of all the other widgets. In the example they create a slew of QLabels with words on them which the user can drag around to create sentences.

The main job is to implement the following functions.

void dragEnterEvent(QDragEnterEvent *event);
void dragMoveEvent(QDragMoveEvent *event);
void dropEvent(QDropEvent *event);
void mousePressEvent(QMouseEvent *event);

This example was introduced with Qt4, but it should give you the idea, the code is here

Basically, when the mouse is pressed the control under the mouse is hidden, and an image is placed in the drag data. When the user drops it somewhere in the control the example closes the one at the previous location and creates a new QLabel and puts it where the user dropped. You should probably change that to just move your control to that location.

The example makes use of a few concepts that are a little different than what was available in 3.3, so here is a drag and drop example from 3.3

The main issue is what to show when the user is dragging around. In the first example, they use a nice trick to build an image of the label, so it really looks like the user is dragging the widget around.

Obviously, it would be much easier to use the MDI functionality in Qt4, but if that's not an option, this is the only thing I can think of.

Hope that helps.