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/examples/tabdialog.qdoc -->
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>
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">Tab Dialog Example</h1>
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>
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> class TabDialog : public QDialog
38
TabDialog(const QString &fileName, QWidget *parent = 0);
41
QTabWidget *tabWidget;
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> TabDialog::TabDialog(const QString &fileName, QWidget *parent)
50
QFileInfo fileInfo(fileName);
52
tabWidget = new QTabWidget;
53
tabWidget->addTab(new GeneralTab(fileInfo), tr("General"));
54
tabWidget->addTab(new PermissionsTab(fileInfo), tr("Permissions"));
55
tabWidget->addTab(new ApplicationsTab(fileInfo), tr("Applications"));</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> QPushButton *okButton = new QPushButton(tr("OK"));
59
QPushButton *cancelButton = new QPushButton(tr("Cancel"));
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> QHBoxLayout *buttonLayout = new QHBoxLayout;
65
buttonLayout->addStretch(1);
66
buttonLayout->addWidget(okButton);
67
buttonLayout->addWidget(cancelButton);
69
QVBoxLayout *mainLayout = new QVBoxLayout;
70
mainLayout->addWidget(tabWidget);
71
mainLayout->addLayout(buttonLayout);</pre>
72
<p>Finally, we set the dialog's title:</p>
73
<pre> setWindowTitle(tr("Tab Dialog"));
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> class GeneralTab : public QWidget
84
GeneralTab(const QFileInfo &fileInfo, QWidget *parent = 0);
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> GeneralTab::GeneralTab(const QFileInfo &fileInfo, QWidget *parent)
92
QLabel *fileNameLabel = new QLabel(tr("File Name:"));
93
QLineEdit *fileNameEdit = new QLineEdit(fileInfo.fileName());
95
QLabel *pathLabel = new QLabel(tr("Path:"));
96
QLabel *pathValueLabel = new QLabel(fileInfo.absoluteFilePath());
97
pathValueLabel->setFrameStyle(QFrame::Panel | QFrame::Sunken);
99
QLabel *sizeLabel = new QLabel(tr("Size:"));
100
qlonglong size = fileInfo.size()/1024;
101
QLabel *sizeValueLabel = new QLabel(tr("%1 K").arg(size));
102
sizeValueLabel->setFrameStyle(QFrame::Panel | QFrame::Sunken);
104
QLabel *lastReadLabel = new QLabel(tr("Last Read:"));
105
QLabel *lastReadValueLabel = new QLabel(fileInfo.lastRead().toString());
106
lastReadValueLabel->setFrameStyle(QFrame::Panel | QFrame::Sunken);
108
QLabel *lastModLabel = new QLabel(tr("Last Modified:"));
109
QLabel *lastModValueLabel = new QLabel(fileInfo.lastModified().toString());
110
lastModValueLabel->setFrameStyle(QFrame::Panel | QFrame::Sunken);
112
QVBoxLayout *mainLayout = new QVBoxLayout;
113
mainLayout->addWidget(fileNameLabel);
114
mainLayout->addWidget(fileNameEdit);
115
mainLayout->addWidget(pathLabel);
116
mainLayout->addWidget(pathValueLabel);
117
mainLayout->addWidget(sizeLabel);
118
mainLayout->addWidget(sizeValueLabel);
119
mainLayout->addWidget(lastReadLabel);
120
mainLayout->addWidget(lastReadValueLabel);
121
mainLayout->addWidget(lastModLabel);
122
mainLayout->addWidget(lastModValueLabel);
123
mainLayout->addStretch(1);
124
setLayout(mainLayout);
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> class PermissionsTab : public QWidget
134
PermissionsTab(const QFileInfo &fileInfo, QWidget *parent = 0);
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> PermissionsTab::PermissionsTab(const QFileInfo &fileInfo, QWidget *parent)
142
QGroupBox *permissionsGroup = new QGroupBox(tr("Permissions"));
144
QCheckBox *readable = new QCheckBox(tr("Readable"));
145
if (fileInfo.isReadable())
146
readable->setChecked(true);
148
QCheckBox *writable = new QCheckBox(tr("Writable"));
149
if ( fileInfo.isWritable() )
150
writable->setChecked(true);
152
QCheckBox *executable = new QCheckBox(tr("Executable"));
153
if ( fileInfo.isExecutable() )
154
executable->setChecked(true);
156
QGroupBox *ownerGroup = new QGroupBox(tr("Ownership"));
158
QLabel *ownerLabel = new QLabel(tr("Owner"));
159
QLabel *ownerValueLabel = new QLabel(fileInfo.owner());
160
ownerValueLabel->setFrameStyle(QFrame::Panel | QFrame::Sunken);
162
QLabel *groupLabel = new QLabel(tr("Group"));
163
QLabel *groupValueLabel = new QLabel(fileInfo.group());
164
groupValueLabel->setFrameStyle(QFrame::Panel | QFrame::Sunken);
166
QVBoxLayout *permissionsLayout = new QVBoxLayout;
167
permissionsLayout->addWidget(readable);
168
permissionsLayout->addWidget(writable);
169
permissionsLayout->addWidget(executable);
170
permissionsGroup->setLayout(permissionsLayout);
172
QVBoxLayout *ownerLayout = new QVBoxLayout;
173
ownerLayout->addWidget(ownerLabel);
174
ownerLayout->addWidget(ownerValueLabel);
175
ownerLayout->addWidget(groupLabel);
176
ownerLayout->addWidget(groupValueLabel);
177
ownerGroup->setLayout(ownerLayout);
179
QVBoxLayout *mainLayout = new QVBoxLayout;
180
mainLayout->addWidget(permissionsGroup);
181
mainLayout->addWidget(ownerGroup);
182
mainLayout->addStretch(1);
183
setLayout(mainLayout);
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> class ApplicationsTab : public QWidget
193
ApplicationsTab(const QFileInfo &fileInfo, QWidget *parent = 0);
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> ApplicationsTab::ApplicationsTab(const QFileInfo &fileInfo, QWidget *parent)
201
QLabel *topLabel = new QLabel(tr("Open with:"));
203
QListWidget *applicationsListBox = new QListWidget;
204
QStringList applications;
206
for (int i = 1; i <= 30; ++i)
207
applications.append(tr("Application %1").arg(i));
208
applicationsListBox->insertItems(0, applications);
210
QCheckBox *alwaysCheckBox;
212
if (fileInfo.suffix().isEmpty())
213
alwaysCheckBox = new QCheckBox(tr("Always use this application to "
214
"open this type of file"));
216
alwaysCheckBox = new QCheckBox(tr("Always use this application to "
217
"open files with the extension '%1'").arg(fileInfo.suffix()));
219
QVBoxLayout *layout = new QVBoxLayout;
220
layout->addWidget(topLabel);
221
layout->addWidget(applicationsListBox);
222
layout->addWidget(alwaysCheckBox);
225
<p /><address><hr /><div align="center">
226
<table width="100%" cellspacing="0" border="0"><tr class="address">
227
<td width="30%">Copyright © 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>