1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation (qt-info@nokia.com)
|
---|
6 | **
|
---|
7 | ** This file is part of the documentation of the Qt Toolkit.
|
---|
8 | **
|
---|
9 | ** $QT_BEGIN_LICENSE:FDL$
|
---|
10 | ** Commercial Usage
|
---|
11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
12 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
13 | ** Software or, alternatively, in accordance with the terms contained in a
|
---|
14 | ** written agreement between you and Nokia.
|
---|
15 | **
|
---|
16 | ** GNU Free Documentation License
|
---|
17 | ** Alternatively, this file may be used under the terms of the GNU Free
|
---|
18 | ** Documentation License version 1.3 as published by the Free Software
|
---|
19 | ** Foundation and appearing in the file included in the packaging of this
|
---|
20 | ** file.
|
---|
21 | **
|
---|
22 | ** If you have questions regarding the use of this file, please contact
|
---|
23 | ** Nokia at qt-info@nokia.com.
|
---|
24 | ** $QT_END_LICENSE$
|
---|
25 | **
|
---|
26 | ****************************************************************************/
|
---|
27 |
|
---|
28 | /*!
|
---|
29 | \page restoring-geometry.html
|
---|
30 | \title Restoring a Window's Geometry
|
---|
31 | \brief How to save & restore window geometry.
|
---|
32 | \ingroup best-practices
|
---|
33 |
|
---|
34 | This document describes how to save and restore a \l{Window
|
---|
35 | Geometry}{window's geometry} using the geometry properties. On
|
---|
36 | Windows, this is basically storing the result of
|
---|
37 | QWidget::geometry() and calling QWidget::setGeometry() in the next
|
---|
38 | session before calling \l{QWidget::show()}{show()}.
|
---|
39 |
|
---|
40 | On X11, this might not work because an invisible window does not
|
---|
41 | have a frame yet. The window manager will decorate the window
|
---|
42 | later. When this happens, the window shifts towards the
|
---|
43 | bottom/right corner of the screen depending on the size of the
|
---|
44 | decoration frame. Although X provides a way to avoid this shift,
|
---|
45 | some window managers fail to implement this feature.
|
---|
46 |
|
---|
47 | Since version 4.2, Qt provides functions that saves and restores a
|
---|
48 | window's geometry and state for you. QWidget::saveGeometry()
|
---|
49 | saves the window geometry and maximized/fullscreen state, while
|
---|
50 | QWidget::restoreGeometry() restores it. The restore function also
|
---|
51 | checks if the restored geometry is outside the available screen
|
---|
52 | geometry, and modifies it as appropriate if it is:
|
---|
53 |
|
---|
54 | \snippet doc/src/snippets/code/src_gui_widgets_qmainwindow.cpp 0
|
---|
55 | \snippet doc/src/snippets/code/src_gui_widgets_qmainwindow.cpp 1
|
---|
56 |
|
---|
57 | If those functions are not available or cannot be used, then a
|
---|
58 | workaround is to call \l{QWidget::setGeometry()}{setGeometry()}
|
---|
59 | after \l{QWidget::show()}{show()}. This has the two disadvantages
|
---|
60 | that the widget appears at a wrong place for a millisecond
|
---|
61 | (results in flashing) and that currently only every second window
|
---|
62 | manager gets it right. A safer solution is to store both
|
---|
63 | \l{QWidget::pos()}{pos()} and \l{QWidget::size()}{size()} and to
|
---|
64 | restore the geometry using \l{QWidget::resize()} and
|
---|
65 | \l{QWidget::move()}{move()} before calling
|
---|
66 | \l{QWidget::show()}{show()}, as demonstrated in the
|
---|
67 | \l{mainwindows/application}{Application} example.
|
---|
68 | */
|
---|