| Trolltech | Documentation | Qt Quarterly | « Internationalization Q & A | Providing Context-Sensitive Help » |
| Laying out MDI Children |
| by Mark Summerfield |
Qt provides support for MDI (Multiple Document Interface) through its QWorkspace widget. The workspace provides two strategies for laying out its child widgets. In this article we will show how easy it is to add new layout strategies to QWorkspace.
The layout strategies available from QWorkspace are cascading (cascade()) and tiling (tile()).


#include <qworkspace.h>
class Workspace : public QWorkspace
{
Q_OBJECT
public:
Workspace(QWidget *parent = 0, const char *name = 0)
: QWorkspace(parent, name)
{}
public slots:
void tileVertically();
void tileHorizontally();
};
The implementation of the slots is straightforward:
void Workspace::tileVertically()
{
QWidgetList windows = windowList();
if (windows.count() < 2) {
tile();
return;
}
int wHeight = height() / windows.count();
int y = 0;
for (QWidget *widget = windows.first(); widget; widget = windows.next()) {
widget->parentWidget()->resize(width(), wHeight);
widget->parentWidget()->move(0, y);
y += wHeight;
}
}
If there are less than two child widgets we simply call the normal tile() function. This will do nothing if there are no windows, and will maximize a single window. We calculate the height that each widget will get and then iterate over the list of child widgets.
Notice that geometry changes to an MDI child widget must be applied to its parentWidget(), not to the widget itself. Similarly, if you want to find out the geometry of an MDI child widget you must use its parentWidget(). This also applies to intercepting events for MDI child widgets: you must install your event filter on the parentWidget().

void Workspace::tileHorizontally()
{
QWidgetList windows = windowList();
if (windows.count() < 2) {
tile();
return;
}
int wWidth = width() / windows.count();
int x = 0;
for (QWidget *widget = windows.first(); widget; widget = windows.next()) {
widget->parentWidget()->resize(wWidth, height());
widget->parentWidget()->move(x, 0);
x += wWidth;
}
}

| Copyright © 2003 Trolltech | Trademarks | Providing Context-Sensitive Help » |