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

« back to all changes in this revision

Viewing changes to doc/html/dialogs-tabdialog.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/examples/tabdialog.qdoc -->
 
6
<head>
 
7
    <title>Qt 4.0: Tab Dialog Example</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
</head>
 
15
<body>
 
16
<table border="0" cellpadding="0" cellspacing="0" width="100%">
 
17
<tr>
 
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">&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>
 
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">Tab Dialog Example</h1>
 
21
<p>Files:</p>
 
22
<ul>
 
23
<li><a href="dialogs-tabdialog-tabdialog-cpp.html">dialogs/tabdialog/tabdialog.cpp</a></li>
 
24
<li><a href="dialogs-tabdialog-tabdialog-h.html">dialogs/tabdialog/tabdialog.h</a></li>
 
25
<li><a href="dialogs-tabdialog-main-cpp.html">dialogs/tabdialog/main.cpp</a></li>
 
26
</ul>
 
27
<p>The Tab Dialog example shows how to construct a tab dialog using the <a href="qtabwidget.html">QTabWidget</a> class.</p>
 
28
<p>Dialogs provide an efficient way for the application to communicate with the user, but complex dialogs suffer from the problem that they often take up too much screen area. By using a number of tabs in a dialog, information can be split into different categories, while remaining accessible.</p>
 
29
<center><img src="images/tabdialog-example.png" /></center><p>The Tab Dialog example consists of a single <tt>TabDialog</tt> class that provides three tabs, each containing information about a particular file, and two standard push buttons that are used to accept or reject the contents of the dialog.</p>
 
30
<a name="tabdialog-class-definition"></a>
 
31
<h2>TabDialog Class Definition</h2>
 
32
<p>The <tt>TabDialog</tt> class is a subclass of <a href="qdialog.html">QDialog</a> that displays a <a href="qtabwidget.html">QTabWidget</a> and two standard dialog buttons. The class definition only contain the class constructor and a private data member for the <a href="qtabwidget.html">QTabWidget</a>:</p>
 
33
<pre>&nbsp;   class TabDialog : public QDialog
 
34
    {
 
35
        Q_OBJECT
 
36
 
 
37
    public:
 
38
        TabDialog(const QString &amp;fileName, QWidget *parent = 0);
 
39
 
 
40
    private:
 
41
        QTabWidget *tabWidget;
 
42
    };</pre>
 
43
<p>In the example, the widget will be used as a top-level window, but we define the constructor so that it can take a parent widget. This allows the dialog to be centered on top of an application's main window.</p>
 
44
<a name="tabdialog-class-implementation"></a>
 
45
<h2>TabDialog Class Implementation</h2>
 
46
<p>The constructor calls the <a href="qdialog.html">QDialog</a> constructor and creates a <a href="qfileinfo.html">QFileInfo</a> object for the specified filename.</p>
 
47
<pre>&nbsp;   TabDialog::TabDialog(const QString &amp;fileName, QWidget *parent)
 
48
        : QDialog(parent)
 
49
    {
 
50
        QFileInfo fileInfo(fileName);
 
51
 
 
52
        tabWidget = new QTabWidget;
 
53
        tabWidget-&gt;addTab(new GeneralTab(fileInfo), tr(&quot;General&quot;));
 
54
        tabWidget-&gt;addTab(new PermissionsTab(fileInfo), tr(&quot;Permissions&quot;));
 
55
        tabWidget-&gt;addTab(new ApplicationsTab(fileInfo), tr(&quot;Applications&quot;));</pre>
 
56
<p>The tab widget is populated with three custom widgets that each contain information about the file. We construct each of these without a parent widget because the tab widget will reparent them as they are added to it.</p>
 
57
<p>We create two standard push buttons, and connect each of them to the appropriate slots in the dialog:</p>
 
58
<pre>&nbsp;       QPushButton *okButton = new QPushButton(tr(&quot;OK&quot;));
 
59
        QPushButton *cancelButton = new QPushButton(tr(&quot;Cancel&quot;));
 
60
 
 
61
        connect(okButton, SIGNAL(clicked()), this, SLOT(accept()));
 
62
        connect(cancelButton, SIGNAL(clicked()), this, SLOT(reject()));</pre>
 
63
<p>We arrange the buttons in a horizontal layout with extra space at the left hand side of the widget then arrange the tab widget above the buttons in the dialog:</p>
 
64
<pre>&nbsp;       QHBoxLayout *buttonLayout = new QHBoxLayout;
 
65
        buttonLayout-&gt;addStretch(1);
 
66
        buttonLayout-&gt;addWidget(okButton);
 
67
        buttonLayout-&gt;addWidget(cancelButton);
 
68
 
 
69
        QVBoxLayout *mainLayout = new QVBoxLayout;
 
70
        mainLayout-&gt;addWidget(tabWidget);
 
71
        mainLayout-&gt;addLayout(buttonLayout);</pre>
 
72
<p>Finally, we set the dialog's title:</p>
 
73
<pre>&nbsp;       setWindowTitle(tr(&quot;Tab Dialog&quot;));
 
74
    }</pre>
 
75
<p>Each of the tabs are subclassed from <a href="qwidget.html">QWidget</a>, and only provide constructors.</p>
 
76
<a name="generaltab-class-definition"></a>
 
77
<h2>GeneralTab Class Definition</h2>
 
78
<p>The GeneralTab widget definition is simple because we are only interested in displaying the contents of a widget within a tab:</p>
 
79
<pre>&nbsp;   class GeneralTab : public QWidget
 
80
    {
 
81
        Q_OBJECT
 
82
 
 
83
    public:
 
84
        GeneralTab(const QFileInfo &amp;fileInfo, QWidget *parent = 0);
 
85
    };</pre>
 
86
<a name="generaltab-class-implementation"></a>
 
87
<h2>GeneralTab Class Implementation</h2>
 
88
<p>The GeneralTab widget simply displays some information about the file passed by the TabDialog. Various widgets for this purpose, and these are arranged within a vertical layout:</p>
 
89
<pre>&nbsp;   GeneralTab::GeneralTab(const QFileInfo &amp;fileInfo, QWidget *parent)
 
90
        : QWidget(parent)
 
91
    {
 
92
        QLabel *fileNameLabel = new QLabel(tr(&quot;File Name:&quot;));
 
93
        QLineEdit *fileNameEdit = new QLineEdit(fileInfo.fileName());
 
94
 
 
95
        QLabel *pathLabel = new QLabel(tr(&quot;Path:&quot;));
 
96
        QLabel *pathValueLabel = new QLabel(fileInfo.absoluteFilePath());
 
97
        pathValueLabel-&gt;setFrameStyle(QFrame::Panel | QFrame::Sunken);
 
98
 
 
99
        QLabel *sizeLabel = new QLabel(tr(&quot;Size:&quot;));
 
100
        qlonglong size = fileInfo.size()/1024;
 
101
        QLabel *sizeValueLabel = new QLabel(tr(&quot;%1 K&quot;).arg(size));
 
102
        sizeValueLabel-&gt;setFrameStyle(QFrame::Panel | QFrame::Sunken);
 
103
 
 
104
        QLabel *lastReadLabel = new QLabel(tr(&quot;Last Read:&quot;));
 
105
        QLabel *lastReadValueLabel = new QLabel(fileInfo.lastRead().toString());
 
106
        lastReadValueLabel-&gt;setFrameStyle(QFrame::Panel | QFrame::Sunken);
 
107
 
 
108
        QLabel *lastModLabel = new QLabel(tr(&quot;Last Modified:&quot;));
 
109
        QLabel *lastModValueLabel = new QLabel(fileInfo.lastModified().toString());
 
110
        lastModValueLabel-&gt;setFrameStyle(QFrame::Panel | QFrame::Sunken);
 
111
 
 
112
        QVBoxLayout *mainLayout = new QVBoxLayout;
 
113
        mainLayout-&gt;addWidget(fileNameLabel);
 
114
        mainLayout-&gt;addWidget(fileNameEdit);
 
115
        mainLayout-&gt;addWidget(pathLabel);
 
116
        mainLayout-&gt;addWidget(pathValueLabel);
 
117
        mainLayout-&gt;addWidget(sizeLabel);
 
118
        mainLayout-&gt;addWidget(sizeValueLabel);
 
119
        mainLayout-&gt;addWidget(lastReadLabel);
 
120
        mainLayout-&gt;addWidget(lastReadValueLabel);
 
121
        mainLayout-&gt;addWidget(lastModLabel);
 
122
        mainLayout-&gt;addWidget(lastModValueLabel);
 
123
        mainLayout-&gt;addStretch(1);
 
124
        setLayout(mainLayout);
 
125
    }</pre>
 
126
<a name="permissionstab-class-definition"></a>
 
127
<h2>PermissionsTab Class Definition</h2>
 
128
<p>Like the GeneralTab, the PermissionsTab is just used as a placeholder widget for its children:</p>
 
129
<pre>&nbsp;   class PermissionsTab : public QWidget
 
130
    {
 
131
        Q_OBJECT
 
132
 
 
133
    public:
 
134
        PermissionsTab(const QFileInfo &amp;fileInfo, QWidget *parent = 0);
 
135
    };</pre>
 
136
<a name="permissionstab-class-implementation"></a>
 
137
<h2>PermissionsTab Class Implementation</h2>
 
138
<p>The PermissionsTab shows information about the file's access information, displaying details of the file permissions and owner in widgets that are arranged in nested layouts:</p>
 
139
<pre>&nbsp;   PermissionsTab::PermissionsTab(const QFileInfo &amp;fileInfo, QWidget *parent)
 
140
        : QWidget(parent)
 
141
    {
 
142
        QGroupBox *permissionsGroup = new QGroupBox(tr(&quot;Permissions&quot;));
 
143
 
 
144
        QCheckBox *readable = new QCheckBox(tr(&quot;Readable&quot;));
 
145
        if (fileInfo.isReadable())
 
146
            readable-&gt;setChecked(true);
 
147
 
 
148
        QCheckBox *writable = new QCheckBox(tr(&quot;Writable&quot;));
 
149
        if ( fileInfo.isWritable() )
 
150
            writable-&gt;setChecked(true);
 
151
 
 
152
        QCheckBox *executable = new QCheckBox(tr(&quot;Executable&quot;));
 
153
        if ( fileInfo.isExecutable() )
 
154
            executable-&gt;setChecked(true);
 
155
 
 
156
        QGroupBox *ownerGroup = new QGroupBox(tr(&quot;Ownership&quot;));
 
157
 
 
158
        QLabel *ownerLabel = new QLabel(tr(&quot;Owner&quot;));
 
159
        QLabel *ownerValueLabel = new QLabel(fileInfo.owner());
 
160
        ownerValueLabel-&gt;setFrameStyle(QFrame::Panel | QFrame::Sunken);
 
161
 
 
162
        QLabel *groupLabel = new QLabel(tr(&quot;Group&quot;));
 
163
        QLabel *groupValueLabel = new QLabel(fileInfo.group());
 
164
        groupValueLabel-&gt;setFrameStyle(QFrame::Panel | QFrame::Sunken);
 
165
 
 
166
        QVBoxLayout *permissionsLayout = new QVBoxLayout;
 
167
        permissionsLayout-&gt;addWidget(readable);
 
168
        permissionsLayout-&gt;addWidget(writable);
 
169
        permissionsLayout-&gt;addWidget(executable);
 
170
        permissionsGroup-&gt;setLayout(permissionsLayout);
 
171
 
 
172
        QVBoxLayout *ownerLayout = new QVBoxLayout;
 
173
        ownerLayout-&gt;addWidget(ownerLabel);
 
174
        ownerLayout-&gt;addWidget(ownerValueLabel);
 
175
        ownerLayout-&gt;addWidget(groupLabel);
 
176
        ownerLayout-&gt;addWidget(groupValueLabel);
 
177
        ownerGroup-&gt;setLayout(ownerLayout);
 
178
 
 
179
        QVBoxLayout *mainLayout = new QVBoxLayout;
 
180
        mainLayout-&gt;addWidget(permissionsGroup);
 
181
        mainLayout-&gt;addWidget(ownerGroup);
 
182
        mainLayout-&gt;addStretch(1);
 
183
        setLayout(mainLayout);
 
184
    }</pre>
 
185
<a name="applicationstab-class-definition"></a>
 
186
<h2>ApplicationsTab Class Definition</h2>
 
187
<p>The ApplicationsTab is another placeholder widget that is mostly cosmetic:</p>
 
188
<pre>&nbsp;   class ApplicationsTab : public QWidget
 
189
    {
 
190
        Q_OBJECT
 
191
 
 
192
    public:
 
193
        ApplicationsTab(const QFileInfo &amp;fileInfo, QWidget *parent = 0);
 
194
    };</pre>
 
195
<a name="applicationstab-class-implementation"></a>
 
196
<h2>ApplicationsTab Class Implementation</h2>
 
197
<p>The ApplicationsTab does not show any useful information, but could be used as a template for a more complicated example:</p>
 
198
<pre>&nbsp;   ApplicationsTab::ApplicationsTab(const QFileInfo &amp;fileInfo, QWidget *parent)
 
199
        : QWidget(parent)
 
200
    {
 
201
        QLabel *topLabel = new QLabel(tr(&quot;Open with:&quot;));
 
202
 
 
203
        QListWidget *applicationsListBox = new QListWidget;
 
204
        QStringList applications;
 
205
 
 
206
        for (int i = 1; i &lt;= 30; ++i)
 
207
            applications.append(tr(&quot;Application %1&quot;).arg(i));
 
208
        applicationsListBox-&gt;insertItems(0, applications);
 
209
 
 
210
        QCheckBox *alwaysCheckBox;
 
211
 
 
212
        if (fileInfo.suffix().isEmpty())
 
213
            alwaysCheckBox = new QCheckBox(tr(&quot;Always use this application to &quot;
 
214
                &quot;open this type of file&quot;));
 
215
        else
 
216
            alwaysCheckBox = new QCheckBox(tr(&quot;Always use this application to &quot;
 
217
                &quot;open files with the extension '%1'&quot;).arg(fileInfo.suffix()));
 
218
 
 
219
        QVBoxLayout *layout = new QVBoxLayout;
 
220
        layout-&gt;addWidget(topLabel);
 
221
        layout-&gt;addWidget(applicationsListBox);
 
222
        layout-&gt;addWidget(alwaysCheckBox);
 
223
        setLayout(layout);
 
224
    }</pre>
 
225
<p /><address><hr /><div align="center">
 
226
<table width="100%" cellspacing="0" border="0"><tr class="address">
 
227
<td width="30%">Copyright &copy; 2005 <a href="trolltech.html">Trolltech</a></td>
 
228
<td width="40%" align="center"><a href="trademarks.html">Trademarks</a></td>
 
229
<td width="30%" align="right"><div align="right">Qt 4.0.0</div></td>
 
230
</tr></table></div></address></body>
 
231
</html>