1
<?xml version="1.0" encoding="iso-8859-1"?>
3
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
4
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
5
<!-- /tmp/qt-4.0.0-espenr-1119621036935/qt-x11-opensource-desktop-4.0.0/doc/src/geometry.qdoc -->
7
<title>Qt 4.0: Window Geometry</title>
8
<style>h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm; }
9
a:link { color: #004faf; text-decoration: none }
10
a:visited { color: #672967; text-decoration: none }
11
td.postheader { font-family: sans-serif }
12
tr.address { font-family: sans-serif }
13
body { background: #ffffff; color: black; }</style>
16
<table border="0" cellpadding="0" cellspacing="0" width="100%">
18
<td align="left" valign="top" width="32"><img src="images/qt-logo.png" align="left" width="32" height="32" border="0" /></td>
19
<td width="1"> </td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a> · <a href="classes.html"><font color="#004faf">All Classes</font></a> · <a href="mainclasses.html"><font color="#004faf">Main Classes</font></a> · <a href="annotated.html"><font color="#004faf">Annotated</font></a> · <a href="groups.html"><font color="#004faf">Grouped Classes</font></a> · <a href="functions.html"><font color="#004faf">Functions</font></a></td>
20
<td align="right" valign="top" width="230"><img src="images/trolltech-logo.png" align="right" width="203" height="32" border="0" /></td></tr></table><h1 align="center">Window Geometry</h1>
21
<p><a href="qwidget.html">QWidget</a> provides several functions that deal with a widget's geometry. Some of these functions operate on the pure client area (i.e. the window excluding the window frame), others include the window frame. The differentiation is done in a way that covers the most common usage transparently.</p>
23
<li><b>Including the window frame:</b> <a href="qwidget.html#x-prop">x()</a>, <a href="qwidget.html#y-prop">y()</a>, <a href="qwidget.html#frameGeometry-prop">frameGeometry()</a>, <a href="qwidget.html#pos-prop">pos()</a>, and <a href="qwidget.html#pos-prop">move()</a>.</li>
24
<li><b>Excluding the window frame:</b> <a href="qwidget.html#geometry-prop">geometry()</a>, <a href="qwidget.html#width-prop">width()</a>, <a href="qwidget.html#height-prop">height()</a>, <a href="qwidget.html#rect-prop">rect()</a>, and <a href="qwidget.html#size-prop">size()</a>.</li>
26
<p>Note that the distinction only matters for decorated top-level widgets. For all child widgets, the frame geometry is equal to the widget's client geometry.</p>
27
<p>This diagram shows most of the functions in use: <center><img src="images/geometry.png" alt="Geometry diagram" /></center></p>
29
<ul><li><a href="#x11-peculiarities">X11 Peculiarities</a></li>
30
<li><a href="#restoring-a-window-s-geometry">Restoring a Window's Geometry</a></li>
32
<a name="x11-peculiarities"></a>
33
<h2>X11 Peculiarities</h2>
34
<p>On <a href="winsystem.html#x11">X11</a>, a window does not have a frame until the window manager decorates it. This happens asynchronously at some point in time after calling <a href="qwidget.html#show">QWidget::show</a>() and the first paint event the window receives, or it does not happen at all. Bear in mind that <a href="winsystem.html#x11">X11</a> is policy-free (others call it flexible). Thus you cannot make any safe assumption about the decoration frame your window will get. Basic rule: There's always one user who uses a window manager that breaks your assumption, and who will complain to you.</p>
35
<p>Furthermore, a toolkit cannot simply place windows on the screen. All Qt can do is to send certain hints to the window manager. The window manager, a separate process, may either obey, ignore or misunderstand them. Due to the partially unclear Inter-Client Communication Conventions Manual (ICCCM), window placement is handled quite differently in existing window managers.</p>
36
<p><a href="winsystem.html#x11">X11</a> provides no standard or easy way to get the frame geometry once the window is decorated. Qt solves this problem with nifty heuristics and clever code that works on a wide range of window managers that exist today. Don't be surprised if you find one where <a href="qwidget.html#frameGeometry-prop">QWidget::frameGeometry</a>() returns wrong results though.</p>
37
<p>Nor does <a href="winsystem.html#x11">X11</a> provide a way to maximize a window. <a href="qwidget.html#showMaximized">QWidget::showMaximized</a>() has to emulate the feature. Its result depends on the result of <a href="qwidget.html#frameGeometry-prop">QWidget::frameGeometry</a>() and the capability of the window manager to do proper window placement, neither of which can be guaranteed.</p>
38
<a name="restoring-a-window-s-geometry"></a>
39
<h2>Restoring a Window's Geometry</h2>
40
<p>A common task in modern applications is to restore a window's geometry in a later session. On Windows, this is basically storing the result of <a href="qwidget.html#geometry-prop">QWidget::geometry</a>() and calling <a href="qwidget.html#geometry-prop">QWidget::setGeometry</a>() in the next session before calling <a href="qwidget.html#show">show()</a>. On <a href="winsystem.html#x11">X11</a>, this won't work because an invisible window doesn't have a frame yet. The window manager will decorate the window later. When this happens, the window shifts towards the bottom/right corner of the screen depending on the size of the decoration frame. Although X provides a way to avoid this shift, our tests have shown that almost all window managers fail to implement this feature.</p>
41
<p>A workaround is to call <a href="qwidget.html#geometry-prop">setGeometry()</a> after <a href="qwidget.html#show">show()</a>. This has the two disadvantages that the widget appears at a wrong place for a millisecond (results in flashing) and that currently only every second window manager gets it right. A safer solution is to store both <a href="qwidget.html#pos-prop">pos()</a> and <a href="qwidget.html#size-prop">size()</a> and to restore the geometry using <a href="qwidget.html#size-prop">QWidget::resize</a>() and <a href="qwidget.html#pos-prop">move()</a> before calling <a href="qwidget.html#show">show()</a>, as demonstrated in the following code snippets (from the <a href="mainwindows-application.html">Application</a> example):</p>
42
<pre> void MainWindow::readSettings()
44
QSettings settings("Trolltech", "Application Example");
45
QPoint pos = settings.value("pos", QPoint(200, 200)).toPoint();
46
QSize size = settings.value("size", QSize(400, 400)).toSize();
51
void MainWindow::writeSettings()
53
QSettings settings("Trolltech", "Application Example");
54
settings.setValue("pos", pos());
55
settings.setValue("size", size());
57
<p>This method works on both Microsoft Windows and most existing <a href="winsystem.html#x11">X11</a> window managers.</p>
58
<p /><address><hr /><div align="center">
59
<table width="100%" cellspacing="0" border="0"><tr class="address">
60
<td width="30%">Copyright © 2005 <a href="trolltech.html">Trolltech</a></td>
61
<td width="40%" align="center"><a href="trademarks.html">Trademarks</a></td>
62
<td width="30%" align="right"><div align="right">Qt 4.0.0</div></td>
63
</tr></table></div></address></body>