~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to doc/html/qt4-mainwindow.html

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?xml version="1.0" encoding="iso-8859-1"?>
 
2
<!DOCTYPE html
 
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/qt4-mainwindow.qdoc -->
 
6
<head>
 
7
    <title>Qt 4.0: The Qt 4 Main Window Classes</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>
 
14
    <link rel="prev" href="qt4-scribe.html" />
 
15
    <link rel="contents" href="qt4-intro.html" />
 
16
    <link rel="next" href="qt4-designer.html" />
 
17
</head>
 
18
<body>
 
19
<table border="0" cellpadding="0" cellspacing="0" width="100%">
 
20
<tr>
 
21
<td align="left" valign="top" width="32"><img src="images/qt-logo.png" align="left" width="32" height="32" border="0" /></td>
 
22
<td width="1">&nbsp;&nbsp;</td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a>&nbsp;&middot; <a href="classes.html"><font color="#004faf">All&nbsp;Classes</font></a>&nbsp;&middot; <a href="mainclasses.html"><font color="#004faf">Main&nbsp;Classes</font></a>&nbsp;&middot; <a href="annotated.html"><font color="#004faf">Annotated</font></a>&nbsp;&middot; <a href="groups.html"><font color="#004faf">Grouped&nbsp;Classes</font></a>&nbsp;&middot; <a href="functions.html"><font color="#004faf">Functions</font></a></td>
 
23
<td align="right" valign="top" width="230"><img src="images/trolltech-logo.png" align="right" width="203" height="32" border="0" /></td></tr></table><p>
 
24
[Previous: <a href="qt4-scribe.html">The Scribe Classes</a>]
 
25
[<a href="qt4-intro.html">Home</a>]
 
26
[Next: <a href="qt4-designer.html">The New Qt Designer</a>]
 
27
</p>
 
28
<h1 align="center">The Qt 4 Main Window Classes</h1>
 
29
<p>Qt 4 introduces a new set of main window classes that will replace the Qt 3 main window classes, providing a more efficient implementation while remaining easy to use.</p>
 
30
<ul><li><a href="#overview-of-the-main-window-classes">Overview of the Main Window Classes</a></li>
 
31
<li><a href="#the-current-state-of-the-main-window-classes">The Current State of the Main Window Classes</a></li>
 
32
<li><a href="#what-s-changed-since-qt-4-beta-1">What's Changed Since Qt 4 Beta 1?</a></li>
 
33
<li><a href="#example-code">Example Code</a></li>
 
34
<li><a href="#what-s-changed-since-qt-3">What's Changed since Qt 3?</a></li>
 
35
</ul>
 
36
<a name="overview-of-the-main-window-classes"></a>
 
37
<h2>Overview of the Main Window Classes</h2>
 
38
<p>The main window-related classes have been redesigned to satisfy a number of requirements, addressing issues raised by our customers and internal developers. The aim of this redesign is to provide a more consistent and efficient framework for main window management.</p>
 
39
<p>Toolbar and dock window functionality is provided by two independent classes: <a href="qtoolbar.html">QToolBar</a> and <a href="qdockwidget.html">QDockWidget</a>. Toolbars and dock widgets reside in separate areas, with toolbars outside the dock widget area. This behavior differs from the standard Qt 3 behavior, where <a href="qtoolbar.html">QToolBar</a> inherited functionality from <a href="qdockwidget.html">QDockWidget</a>, and both types of component shared the same areas. The result is a more consistent and predictable experience for users. Toolbars and dock widgets provide feedback while being dragged into their new positions.</p>
 
40
<center><img src="images/mainwindow-docks.png" /></center><p>The diagram above shows the layout of a main window that contains both toolbars and dock widgets. Each corner area can be used by either of the adjacent dock widget areas, allowing dock widget behavior and main window layout to be specified precisely.</p>
 
41
<p>Toolbars and dock widgets are child widgets of the main window. They are no longer reparented into a dock area widget by the main window. Instead, layouts are used to manage the placement of toolbars and dock widgets. One consequence is that the old <a href="porting4.html#qdockarea">QDockArea</a> class is no longer required in Qt 4.</p>
 
42
<a name="the-current-state-of-the-main-window-classes"></a>
 
43
<h2>The Current State of the Main Window Classes</h2>
 
44
<p>Qt 4 provides the following classes for managing main windows and associated user interface components:</p>
 
45
<ul>
 
46
<li><a href="qmainwindow.html">QMainWindow</a> remains the central class around which applications can be built. The interface to this class has been simplified, and much of the functionality previously included in this class is now present in the companion <a href="qdockwidget.html">QDockWidget</a> and <a href="qtoolbar.html">QToolBar</a> classes.</li>
 
47
<li><a href="qdockwidget.html">QDockWidget</a> provides a widget that can be used to create detachable tool palettes or helper windows. Dock widgets keep track of their own properties, and they can be moved, closed, and floated as external windows.</li>
 
48
<li><a href="qtoolbar.html">QToolBar</a> provides a generic toolbar widget that can hold a number of different action-related widgets, such as buttons, drop-down menus, comboboxes, and spin boxes. The emphasis on a unified action model in Qt 4 means that toolbars cooperate well with menus and keyboard shortcuts.</li>
 
49
</ul>
 
50
<a name="what-s-changed-since-qt-4-beta-1"></a>
 
51
<h2>What's Changed Since Qt 4 Beta 1?</h2>
 
52
<ul>
 
53
<li><a href="qmainwindow.html">QMainWindow</a> and <a href="qtoolbar.html">QToolBar</a> have an <tt>iconSize</tt> property that specifies the size of the <a href="qtoolbutton.html">QToolButton</a> icons. The default size is chosen based on the style.</li>
 
54
<li>The behavior of <a href="qdockwidget.html">QDockWidget</a> has been improved significantly.</li>
 
55
</ul>
 
56
<a name="example-code"></a>
 
57
<h2>Example Code</h2>
 
58
<p>Using <a href="qmainwindow.html">QMainWindow</a> is straightforward. Generally, we subclass <a href="qmainwindow.html">QMainWindow</a> and set up menus, toolbars, and dock widgets inside the <a href="qmainwindow.html">QMainWindow</a> constructor.</p>
 
59
<p>To add a menu bar to the main window, we simply create the menus, and add them to the main window's menu bar. Note that the <a href="qmainwindow.html#menuBar">QMainWindow::menuBar</a>() function will automatically create the menu bar the first time it is called. You can also call <a href="qmainwindow.html#setMenuBar">QMainWindow::setMenuBar</a>() to use a custom menu bar in the main window.</p>
 
60
<pre>&nbsp;   MainWindow::MainWindow(QWidget *parent)
 
61
        : QMainWindow(parent)
 
62
    {
 
63
        ...
 
64
        newAct = new QAction(tr(&quot;&amp;New&quot;), this);
 
65
        newAct-&gt;setShortcut(tr(&quot;Ctrl+N&quot;));
 
66
        newAct-&gt;setStatusTip(tr(&quot;Create a new file&quot;));
 
67
        connect(newAct, SIGNAL(triggered()), this, SLOT(newFile()));
 
68
 
 
69
        openAct = new QAction(tr(&quot;&amp;Open...&quot;), this);
 
70
        openAct-&gt;setShortcut(tr(&quot;Ctrl+O&quot;));
 
71
        openAct-&gt;setStatusTip(tr(&quot;Open an existing file&quot;));
 
72
        connect(openAct, SIGNAL(triggered()), this, SLOT(open()));
 
73
        ...</pre>
 
74
<p>Once actions have been created, we can add them to the main window components. To begin with, we add them to the popup menus:</p>
 
75
<pre>&nbsp;       fileMenu = menuBar()-&gt;addMenu(tr(&quot;&amp;File&quot;));
 
76
        fileMenu-&gt;addAction(newAct);
 
77
        fileMenu-&gt;addAction(openAct);
 
78
        ...
 
79
        fileMenu-&gt;addSeparator();
 
80
        ...</pre>
 
81
<pre>&nbsp;   }</pre>
 
82
<p>The <a href="qtoolbar.html">QToolBar</a> and <a href="qmenu.html">QMenu</a> classes use Qt's action system to provide a consistent API. In the above code, some existing actions were added to the file menu with the <a href="qmenu.html#addAction">QMenu::addAction</a>() function. <a href="qtoolbar.html">QToolBar</a> also provides this function, making it easy to reuse actions in different parts of the main window. This avoids unnecessary duplication of work.</p>
 
83
<p>We create a toolbar as a child of the main window, and add the desired actions to it:</p>
 
84
<pre>&nbsp;       fileToolBar = addToolBar(tr(&quot;File&quot;));
 
85
        fileToolBar-&gt;addAction(newAct);
 
86
        fileToolBar-&gt;addAction(openAct);
 
87
        ...</pre>
 
88
<pre>&nbsp;       fileToolbar-&gt;setAllowedAreas(Qt::TopToolBarArea | Qt::BottomToolBarArea);
 
89
        fileToolbar-&gt;setArea(Qt::TopToolBarArea);</pre>
 
90
<p>In this example, the toolbar is restricted to the top and bottom toolbar areas of the main window, and is initially placed in the top tool bar area. We can see that the actions specified by <tt>openAction</tt> and <tt>saveAction</tt> will be displayed both on the toolbar and in the file menu.</p>
 
91
<p><a href="qdockwidget.html">QDockWidget</a> is used in a similar way to <a href="qtoolbar.html">QToolBar</a>. We create a dock widget as a child of the main window, and add widgets as children of the dock widget:</p>
 
92
<pre>&nbsp;       contentsWindow = new QDockWidget(tr(&quot;Table of Contents&quot;), this);
 
93
        contentsWindow-&gt;setAllowedAreas(Qt::LeftDockWidgetArea
 
94
                                      | Qt::RightDockWidgetArea);
 
95
        addDockWidget(Qt::LeftDockWidgetArea, contentsWindow);
 
96
 
 
97
        headingList = new QListWidget(contentsWindow);
 
98
        contentsWindow-&gt;setWidget(headingList);</pre>
 
99
<p>In this example, the dock widget can only be placed in the left and right dock areas, and it is initially placed in the left dock area.</p>
 
100
<p>The <a href="qmainwindow.html">QMainWindow</a> API allows the programmer to customize which dock widget areas occupy the four corners of the dock widget area. If required, the default can be changed with the <a href="qmainwindow.html#setCorner">QMainWindow::setCorner</a>() function:</p>
 
101
<pre>&nbsp;   setCorner(Qt::TopLeftCorner, Qt::LeftDockWidgetArea);
 
102
    setCorner(Qt::BottomLeftCorner, Qt::LeftDockWidgetArea);
 
103
    setCorner(Qt::TopRightCorner, Qt::RightDockWidgetArea);
 
104
    setCorner(Qt::BottomRightCorner, Qt::RightDockWidgetArea);</pre>
 
105
<p>The following diagram shows the configuration produced by the above code. Note that the left and right dock widgets will occupy the top and bottom corners of the main window in this layout.</p>
 
106
<center><img src="images/mainwindow-docks-example.png" /></center><p>Once all of the main window components have been set up, the central widget is created and installed by using code similar to the following:</p>
 
107
<pre>&nbsp;   QWidget *centralWidget = new QWidget(this);
 
108
    setCentralWidget(centralWidget);</pre>
 
109
<p>The central widget can be any subclass of <a href="qwidget.html">QWidget</a>.</p>
 
110
<a name="what-s-changed-since-qt-3"></a>
 
111
<h2>What's Changed since Qt 3?</h2>
 
112
<p>Although the <a href="qmainwindow.html">QMainWindow</a> class in Qt 3 provided support for toolbars, dock widgets, and other standard user interface components, its design meant that these items were managed through a large number of <a href="qmainwindow.html">QMainWindow</a> member functions. In Qt 4, the <a href="qmainwindow.html">QMainWindow</a> class delegates many of the management tasks to <a href="qdockwidget.html">QDockWidget</a> and <a href="qtoolbar.html">QToolBar</a>. As a result, these classes are used in a slightly different way in Qt 4.</p>
 
113
<p><a href="qmainwindow.html">QMainWindow</a> retains the menuBar() function, but menus are always constructed using <a href="qaction.html">QAction</a> objects. All kinds of menus are constructed using the general <a href="qmenu.html">QMenu</a> class.</p>
 
114
<p>Qt 3:</p>
 
115
<pre>&nbsp;   QPopupMenu *fileMenu = new QPopupMenu(this);
 
116
    openAction-&gt;addTo(fileMenu);
 
117
    saveAction-&gt;addTo(fileMenu);
 
118
    ...
 
119
    menuBar()-&gt;insertItem(tr(&quot;&amp;File&quot;), fileMenu);</pre>
 
120
<p>Qt 4:</p>
 
121
<pre>&nbsp;   QMenu *fileMenu = menuBar()-&gt;addMenu(tr(&quot;&amp;File&quot;));
 
122
    fileMenu-&gt;addAction(openAction);
 
123
    fileMenu-&gt;addAction(saveAction);
 
124
    ...</pre>
 
125
<p>Toolbars follow the same pattern as menus, with the new, more consistent behavior:</p>
 
126
<p>Qt 3:</p>
 
127
<pre>&nbsp;   QToolBar *fileTools = new QToolBar(this, &quot;file toolbar&quot;);
 
128
    openAction-&gt;addTo(fileTools);
 
129
    saveAction-&gt;addTo(fileTools);
 
130
    ...</pre>
 
131
<p>Qt 4:</p>
 
132
<pre>&nbsp;   QToolBar *fileTools = addToolBar(tr(&quot;File Tool Bar&quot;));
 
133
    fileTools-&gt;addAction(openAction);
 
134
    fileTools-&gt;addAction(saveAction);
 
135
    ...</pre>
 
136
<p>The behavior of dock widgets is now configured through the member functions of <a href="qdockwidget.html">QDockWidget</a>. For example, compare the old and new ways of creating a dock widget in the dock area on the left hand side of the main window.</p>
 
137
<p>In Qt 3:</p>
 
138
<pre>&nbsp;   QDockWidget *dockWidget = new QDockWidget(this);
 
139
    mainWin-&gt;moveDockWidget(dockWidget, Qt::DockLeft);</pre>
 
140
<p>In Qt 4:</p>
 
141
<pre>&nbsp;   QDockWidget *dockWidget = new QDockWidget(mainWindow);
 
142
    mainWindow-&gt;addDockWidget(Qt::LeftDockWidgetArea, dockWidget);</pre>
 
143
<p>These changes provide more than a cosmetic improvement to Qt's API. They allow more consistent behavior to be defined and implemented. In response to feedback from customers, we hope to improve these classes even further.</p>
 
144
<p>
 
145
[Previous: <a href="qt4-scribe.html">The Scribe Classes</a>]
 
146
[<a href="qt4-intro.html">Home</a>]
 
147
[Next: <a href="qt4-designer.html">The New Qt Designer</a>]
 
148
</p>
 
149
<p /><address><hr /><div align="center">
 
150
<table width="100%" cellspacing="0" border="0"><tr class="address">
 
151
<td width="30%">Copyright &copy; 2005 <a href="trolltech.html">Trolltech</a></td>
 
152
<td width="40%" align="center"><a href="trademarks.html">Trademarks</a></td>
 
153
<td width="30%" align="right"><div align="right">Qt 4.0.0</div></td>
 
154
</tr></table></div></address></body>
 
155
</html>