1 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
|
---|
2 | <!-- /home/espenr/tmp/qt-3.3.8-espenr-2499/qt-x11-free-3.3.8/examples/addressbook/addressbook.doc:4 -->
|
---|
3 | <html>
|
---|
4 | <head>
|
---|
5 | <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
---|
6 | <title>Simple Addressbook</title>
|
---|
7 | <style type="text/css"><!--
|
---|
8 | fn { margin-left: 1cm; text-indent: -1cm; }
|
---|
9 | a:link { color: #004faf; text-decoration: none }
|
---|
10 | a:visited { color: #672967; text-decoration: none }
|
---|
11 | body { background: #ffffff; color: black; }
|
---|
12 | --></style>
|
---|
13 | </head>
|
---|
14 | <body>
|
---|
15 |
|
---|
16 | <table border="0" cellpadding="0" cellspacing="0" width="100%">
|
---|
17 | <tr bgcolor="#E5E5E5">
|
---|
18 | <td valign=center>
|
---|
19 | <a href="index.html">
|
---|
20 | <font color="#004faf">Home</font></a>
|
---|
21 | | <a href="classes.html">
|
---|
22 | <font color="#004faf">All Classes</font></a>
|
---|
23 | | <a href="mainclasses.html">
|
---|
24 | <font color="#004faf">Main Classes</font></a>
|
---|
25 | | <a href="annotated.html">
|
---|
26 | <font color="#004faf">Annotated</font></a>
|
---|
27 | | <a href="groups.html">
|
---|
28 | <font color="#004faf">Grouped Classes</font></a>
|
---|
29 | | <a href="functions.html">
|
---|
30 | <font color="#004faf">Functions</font></a>
|
---|
31 | </td>
|
---|
32 | <td align="right" valign="center"><img src="logo32.png" align="right" width="64" height="32" border="0"></td></tr></table><h1 align=center>Simple Addressbook</h1>
|
---|
33 |
|
---|
34 |
|
---|
35 | <p>
|
---|
36 | This examples shows how to write a very simple, but complete application
|
---|
37 | using an addressbook as the example.
|
---|
38 | <p> <hr>
|
---|
39 | <p> Header file of the mainwindow:
|
---|
40 | <p> <pre>/****************************************************************************
|
---|
41 | ** $Id: addressbook-example.html 2051 2007-02-21 10:04:20Z chehrlic $
|
---|
42 | **
|
---|
43 | ** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.
|
---|
44 | **
|
---|
45 | ** This file is part of an example program for Qt. This example
|
---|
46 | ** program may be used, distributed and modified without limitation.
|
---|
47 | **
|
---|
48 | *****************************************************************************/
|
---|
49 |
|
---|
50 | #ifndef AB_MAINWINDOW_H
|
---|
51 | #define AB_MAINWINDOW_H
|
---|
52 |
|
---|
53 | #include <<a href="qmainwindow-h.html">qmainwindow.h</a>>
|
---|
54 | #include <<a href="qstring-h.html">qstring.h</a>>
|
---|
55 |
|
---|
56 | class QToolBar;
|
---|
57 | class QPopupMenu;
|
---|
58 | class ABCentralWidget;
|
---|
59 |
|
---|
60 | class ABMainWindow: public <a href="qmainwindow.html">QMainWindow</a>
|
---|
61 | {
|
---|
62 | <a href="metaobjects.html#Q_OBJECT">Q_OBJECT</a>
|
---|
63 |
|
---|
64 | public:
|
---|
65 | ABMainWindow();
|
---|
66 | ~ABMainWindow();
|
---|
67 |
|
---|
68 | protected slots:
|
---|
69 | void fileNew();
|
---|
70 | void fileOpen();
|
---|
71 | void fileSave();
|
---|
72 | void fileSaveAs();
|
---|
73 | void filePrint();
|
---|
74 | void closeWindow();
|
---|
75 |
|
---|
76 | protected:
|
---|
77 | void setupMenuBar();
|
---|
78 | void setupFileTools();
|
---|
79 | void setupStatusBar();
|
---|
80 | void setupCentralWidget();
|
---|
81 |
|
---|
82 | <a href="qtoolbar.html">QToolBar</a> *fileTools;
|
---|
83 | <a href="qstring.html">QString</a> filename;
|
---|
84 | ABCentralWidget *view;
|
---|
85 |
|
---|
86 | };
|
---|
87 |
|
---|
88 |
|
---|
89 | #endif
|
---|
90 | </pre>
|
---|
91 |
|
---|
92 | <p> <hr>
|
---|
93 | <p> Implementation of the mainwindow:
|
---|
94 | <p> <pre>/****************************************************************************
|
---|
95 | ** $Id: addressbook-example.html 2051 2007-02-21 10:04:20Z chehrlic $
|
---|
96 | **
|
---|
97 | ** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.
|
---|
98 | **
|
---|
99 | ** This file is part of an example program for Qt. This example
|
---|
100 | ** program may be used, distributed and modified without limitation.
|
---|
101 | **
|
---|
102 | *****************************************************************************/
|
---|
103 |
|
---|
104 | #include "mainwindow.h"
|
---|
105 | #include "centralwidget.h"
|
---|
106 |
|
---|
107 | #include <<a href="qtoolbar-h.html">qtoolbar.h</a>>
|
---|
108 | #include <<a href="qtoolbutton-h.html">qtoolbutton.h</a>>
|
---|
109 | #include <<a href="qpopupmenu-h.html">qpopupmenu.h</a>>
|
---|
110 | #include <<a href="qmenubar-h.html">qmenubar.h</a>>
|
---|
111 | #include <<a href="qstatusbar-h.html">qstatusbar.h</a>>
|
---|
112 | #include <<a href="qapplication-h.html">qapplication.h</a>>
|
---|
113 | #include <<a href="qfiledialog-h.html">qfiledialog.h</a>>
|
---|
114 |
|
---|
115 | <a name="f263"></a>ABMainWindow::ABMainWindow()
|
---|
116 | : <a href="qmainwindow.html">QMainWindow</a>( 0, "example addressbook application" ),
|
---|
117 | filename( <a href="qstring.html#QString-null">QString::null</a> )
|
---|
118 | {
|
---|
119 | setupMenuBar();
|
---|
120 | setupFileTools();
|
---|
121 | setupStatusBar();
|
---|
122 | setupCentralWidget();
|
---|
123 | }
|
---|
124 |
|
---|
125 |
|
---|
126 | ABMainWindow::~ABMainWindow()
|
---|
127 | {
|
---|
128 | }
|
---|
129 |
|
---|
130 | void <a name="f264"></a>ABMainWindow::setupMenuBar()
|
---|
131 | {
|
---|
132 | <a href="qpopupmenu.html">QPopupMenu</a> *file = new <a href="qpopupmenu.html">QPopupMenu</a>( this );
|
---|
133 | <a href="qmainwindow.html#menuBar">menuBar</a>()->insertItem( "&File", file );
|
---|
134 |
|
---|
135 | <a name="x569"></a> file-><a href="qmenudata.html#insertItem">insertItem</a>( "New", this, SLOT( fileNew() ), CTRL + Key_N );
|
---|
136 | file-><a href="qmenudata.html#insertItem">insertItem</a>( QPixmap( "fileopen.xpm" ), "Open", this, SLOT( fileOpen() ), CTRL + Key_O );
|
---|
137 | <a name="x570"></a> file-><a href="qmenudata.html#insertSeparator">insertSeparator</a>();
|
---|
138 | file-><a href="qmenudata.html#insertItem">insertItem</a>( QPixmap( "filesave.xpm" ), "Save", this, SLOT( fileSave() ), CTRL + Key_S );
|
---|
139 | file-><a href="qmenudata.html#insertItem">insertItem</a>( "Save As...", this, SLOT( fileSaveAs() ) );
|
---|
140 | file-><a href="qmenudata.html#insertSeparator">insertSeparator</a>();
|
---|
141 | file-><a href="qmenudata.html#insertItem">insertItem</a>( QPixmap( "fileprint.xpm" ), "Print...", this, SLOT( filePrint() ), CTRL + Key_P );
|
---|
142 | file-><a href="qmenudata.html#insertSeparator">insertSeparator</a>();
|
---|
143 | file-><a href="qmenudata.html#insertItem">insertItem</a>( "Close", this, SLOT( closeWindow() ), CTRL + Key_W );
|
---|
144 | file-><a href="qmenudata.html#insertItem">insertItem</a>( "Quit", qApp, SLOT( <a href="qapplication.html#quit">quit</a>() ), CTRL + Key_Q );
|
---|
145 | }
|
---|
146 |
|
---|
147 | void <a name="f265"></a>ABMainWindow::setupFileTools()
|
---|
148 | {
|
---|
149 | //fileTools = new <a href="qtoolbar.html">QToolBar</a>( this, "file operations" );
|
---|
150 | }
|
---|
151 |
|
---|
152 | void <a name="f266"></a>ABMainWindow::setupStatusBar()
|
---|
153 | {
|
---|
154 | //statusBar()->message( "Ready", 2000 );
|
---|
155 | }
|
---|
156 |
|
---|
157 | void <a name="f267"></a>ABMainWindow::setupCentralWidget()
|
---|
158 | {
|
---|
159 | view = new ABCentralWidget( this );
|
---|
160 | <a href="qmainwindow.html#setCentralWidget">setCentralWidget</a>( view );
|
---|
161 | }
|
---|
162 |
|
---|
163 | void <a name="f268"></a>ABMainWindow::closeWindow()
|
---|
164 | {
|
---|
165 | <a href="qwidget.html#close">close</a>();
|
---|
166 | }
|
---|
167 |
|
---|
168 | void <a name="f269"></a>ABMainWindow::fileNew()
|
---|
169 | {
|
---|
170 | }
|
---|
171 |
|
---|
172 | void <a name="f270"></a>ABMainWindow::fileOpen()
|
---|
173 | {
|
---|
174 | <a name="x567"></a> <a href="qstring.html">QString</a> fn = QFileDialog::<a href="qfiledialog.html#getOpenFileName">getOpenFileName</a>( QString::null, QString::null, this );
|
---|
175 | <a name="x571"></a> if ( !fn.<a href="qstring.html#isEmpty">isEmpty</a>() ) {
|
---|
176 | filename = fn;
|
---|
177 | view->load( filename );
|
---|
178 | }
|
---|
179 | }
|
---|
180 |
|
---|
181 | void <a name="f271"></a>ABMainWindow::fileSave()
|
---|
182 | {
|
---|
183 | if ( filename.isEmpty() ) {
|
---|
184 | fileSaveAs();
|
---|
185 | return;
|
---|
186 | }
|
---|
187 |
|
---|
188 | view->save( filename );
|
---|
189 | }
|
---|
190 |
|
---|
191 | void <a name="f272"></a>ABMainWindow::fileSaveAs()
|
---|
192 | {
|
---|
193 | <a name="x568"></a> <a href="qstring.html">QString</a> fn = QFileDialog::<a href="qfiledialog.html#getSaveFileName">getSaveFileName</a>( QString::null, QString::null, this );
|
---|
194 | if ( !fn.<a href="qstring.html#isEmpty">isEmpty</a>() ) {
|
---|
195 | filename = fn;
|
---|
196 | fileSave();
|
---|
197 | }
|
---|
198 | }
|
---|
199 |
|
---|
200 | void <a name="f273"></a>ABMainWindow::filePrint()
|
---|
201 | {
|
---|
202 | }
|
---|
203 | </pre>
|
---|
204 |
|
---|
205 | <p> <hr>
|
---|
206 | <p> Header file of the centralwidget:
|
---|
207 | <p> <pre>/****************************************************************************
|
---|
208 | ** $Id: addressbook-example.html 2051 2007-02-21 10:04:20Z chehrlic $
|
---|
209 | **
|
---|
210 | ** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.
|
---|
211 | **
|
---|
212 | ** This file is part of an example program for Qt. This example
|
---|
213 | ** program may be used, distributed and modified without limitation.
|
---|
214 | **
|
---|
215 | *****************************************************************************/
|
---|
216 |
|
---|
217 | #ifndef AB_CENTRALWIDGET_H
|
---|
218 | #define AB_CENTRALWIDGET_H
|
---|
219 |
|
---|
220 | #include <<a href="qwidget-h.html">qwidget.h</a>>
|
---|
221 | #include <<a href="qstring-h.html">qstring.h</a>>
|
---|
222 |
|
---|
223 | class QTabWidget;
|
---|
224 | class QListView;
|
---|
225 | class QGridLayout;
|
---|
226 | class QLineEdit;
|
---|
227 | class QPushButton;
|
---|
228 | class QListViewItem;
|
---|
229 | class QCheckBox;
|
---|
230 |
|
---|
231 | class ABCentralWidget : public <a href="qwidget.html">QWidget</a>
|
---|
232 | {
|
---|
233 | Q_OBJECT
|
---|
234 |
|
---|
235 | public:
|
---|
236 | ABCentralWidget( <a href="qwidget.html">QWidget</a> *parent, const char *name = 0 );
|
---|
237 |
|
---|
238 | void save( const <a href="qstring.html">QString</a> &filename );
|
---|
239 | void load( const <a href="qstring.html">QString</a> &filename );
|
---|
240 |
|
---|
241 | protected slots:
|
---|
242 | void addEntry();
|
---|
243 | void changeEntry();
|
---|
244 | void itemSelected( <a href="qlistviewitem.html">QListViewItem</a>* );
|
---|
245 | void selectionChanged();
|
---|
246 | void toggleFirstName();
|
---|
247 | void toggleLastName();
|
---|
248 | void toggleAddress();
|
---|
249 | void toggleEMail();
|
---|
250 | void findEntries();
|
---|
251 |
|
---|
252 | protected:
|
---|
253 | void setupTabWidget();
|
---|
254 | void setupListView();
|
---|
255 |
|
---|
256 | <a href="qgridlayout.html">QGridLayout</a> *mainGrid;
|
---|
257 | <a href="qtabwidget.html">QTabWidget</a> *tabWidget;
|
---|
258 | <a href="qlistview.html">QListView</a> *listView;
|
---|
259 | <a href="qpushbutton.html">QPushButton</a> *add, *change, *find;
|
---|
260 | <a href="qlineedit.html">QLineEdit</a> *iFirstName, *iLastName, *iAddress, *iEMail,
|
---|
261 | *sFirstName, *sLastName, *sAddress, *sEMail;
|
---|
262 | <a href="qcheckbox.html">QCheckBox</a> *cFirstName, *cLastName, *cAddress, *cEMail;
|
---|
263 |
|
---|
264 | };
|
---|
265 |
|
---|
266 | #endif
|
---|
267 | </pre>
|
---|
268 |
|
---|
269 | <p> <hr>
|
---|
270 | <p> Implementation of the centralwidget:
|
---|
271 | <p> <pre>/****************************************************************************
|
---|
272 | ** $Id: addressbook-example.html 2051 2007-02-21 10:04:20Z chehrlic $
|
---|
273 | **
|
---|
274 | ** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.
|
---|
275 | **
|
---|
276 | ** This file is part of an example program for Qt. This example
|
---|
277 | ** program may be used, distributed and modified without limitation.
|
---|
278 | **
|
---|
279 | *****************************************************************************/
|
---|
280 |
|
---|
281 | #include "centralwidget.h"
|
---|
282 |
|
---|
283 | #include <<a href="qtabwidget-h.html">qtabwidget.h</a>>
|
---|
284 | #include <<a href="qlistview-h.html">qlistview.h</a>>
|
---|
285 | #include <<a href="qlayout-h.html">qlayout.h</a>>
|
---|
286 | #include <<a href="qwidget-h.html">qwidget.h</a>>
|
---|
287 | #include <<a href="qlabel-h.html">qlabel.h</a>>
|
---|
288 | #include <<a href="qpushbutton-h.html">qpushbutton.h</a>>
|
---|
289 | #include <<a href="qlineedit-h.html">qlineedit.h</a>>
|
---|
290 | #include <<a href="qlabel-h.html">qlabel.h</a>>
|
---|
291 | #include <<a href="qcheckbox-h.html">qcheckbox.h</a>>
|
---|
292 | #include <<a href="qfile-h.html">qfile.h</a>>
|
---|
293 | #include <<a href="qtextstream-h.html">qtextstream.h</a>>
|
---|
294 |
|
---|
295 | <a name="f274"></a>ABCentralWidget::ABCentralWidget( <a href="qwidget.html">QWidget</a> *parent, const char *name )
|
---|
296 | : <a href="qwidget.html">QWidget</a>( parent, name )
|
---|
297 | {
|
---|
298 | mainGrid = new <a href="qgridlayout.html">QGridLayout</a>( this, 2, 1, 5, 5 );
|
---|
299 |
|
---|
300 | setupTabWidget();
|
---|
301 | setupListView();
|
---|
302 |
|
---|
303 | <a name="x579"></a> mainGrid-><a href="qgridlayout.html#setRowStretch">setRowStretch</a>( 0, 0 );
|
---|
304 | mainGrid-><a href="qgridlayout.html#setRowStretch">setRowStretch</a>( 1, 1 );
|
---|
305 | }
|
---|
306 |
|
---|
307 | void <a name="f275"></a>ABCentralWidget::save( const <a href="qstring.html">QString</a> &filename )
|
---|
308 | {
|
---|
309 | <a name="x590"></a> if ( !listView-><a href="qlistview.html#firstChild">firstChild</a>() )
|
---|
310 | return;
|
---|
311 |
|
---|
312 | <a href="qfile.html">QFile</a> f( filename );
|
---|
313 | if ( !f.<a href="qfile.html#open">open</a>( <a href="qfile.html#open">IO_WriteOnly</a> ) )
|
---|
314 | return;
|
---|
315 |
|
---|
316 | <a href="qtextstream.html">QTextStream</a> t( &f );
|
---|
317 | <a name="x603"></a> t.<a href="qtextstream.html#setEncoding">setEncoding</a>(QTextStream::UnicodeUTF8);
|
---|
318 |
|
---|
319 | <a href="qlistviewitemiterator.html">QListViewItemIterator</a> it( listView );
|
---|
320 |
|
---|
321 | <a name="x597"></a> for ( ; it.<a href="qlistviewitemiterator.html#current">current</a>(); ++it )
|
---|
322 | for ( unsigned int i = 0; i < 4; i++ )
|
---|
323 | t << it.<a href="qlistviewitemiterator.html#current">current</a>()->text( i ) << "\n";
|
---|
324 |
|
---|
325 | f.<a href="qfile.html#close">close</a>();
|
---|
326 | }
|
---|
327 |
|
---|
328 | void <a name="f276"></a>ABCentralWidget::load( const <a href="qstring.html">QString</a> &filename )
|
---|
329 | {
|
---|
330 | <a name="x586"></a> listView-><a href="qlistview.html#clear">clear</a>();
|
---|
331 |
|
---|
332 | <a href="qfile.html">QFile</a> f( filename );
|
---|
333 | if ( !f.<a href="qfile.html#open">open</a>( <a href="qfile.html#open">IO_ReadOnly</a> ) )
|
---|
334 | return;
|
---|
335 |
|
---|
336 | <a href="qtextstream.html">QTextStream</a> t( &f );
|
---|
337 | t.<a href="qtextstream.html#setEncoding">setEncoding</a>(QTextStream::UnicodeUTF8);
|
---|
338 |
|
---|
339 | <a name="x601"></a> while ( !t.<a href="qtextstream.html#atEnd">atEnd</a>() ) {
|
---|
340 | <a href="qlistviewitem.html">QListViewItem</a> *item = new <a href="qlistviewitem.html">QListViewItem</a>( listView );
|
---|
341 | for ( unsigned int i = 0; i < 4; i++ )
|
---|
342 | <a name="x602"></a><a name="x595"></a> item-><a href="qlistviewitem.html#setText">setText</a>( i, t.<a href="qtextstream.html#readLine">readLine</a>() );
|
---|
343 | }
|
---|
344 |
|
---|
345 | f.<a href="qfile.html#close">close</a>();
|
---|
346 | }
|
---|
347 |
|
---|
348 | void <a name="f277"></a>ABCentralWidget::setupTabWidget()
|
---|
349 | {
|
---|
350 | tabWidget = new <a href="qtabwidget.html">QTabWidget</a>( this );
|
---|
351 |
|
---|
352 | <a href="qwidget.html">QWidget</a> *input = new <a href="qwidget.html">QWidget</a>( tabWidget );
|
---|
353 | <a href="qgridlayout.html">QGridLayout</a> *grid1 = new <a href="qgridlayout.html">QGridLayout</a>( input, 2, 5, 5, 5 );
|
---|
354 |
|
---|
355 | <a href="qlabel.html">QLabel</a> *liFirstName = new <a href="qlabel.html">QLabel</a>( "First &Name", input );
|
---|
356 | <a name="x604"></a><a name="x581"></a> liFirstName-><a href="qwidget.html#resize">resize</a>( liFirstName-><a href="qwidget.html#sizeHint">sizeHint</a>() );
|
---|
357 | <a name="x578"></a> grid1-><a href="qgridlayout.html#addWidget">addWidget</a>( liFirstName, 0, 0 );
|
---|
358 |
|
---|
359 | <a href="qlabel.html">QLabel</a> *liLastName = new <a href="qlabel.html">QLabel</a>( "&Last Name", input );
|
---|
360 | liLastName-><a href="qwidget.html#resize">resize</a>( liLastName-><a href="qwidget.html#sizeHint">sizeHint</a>() );
|
---|
361 | grid1-><a href="qgridlayout.html#addWidget">addWidget</a>( liLastName, 0, 1 );
|
---|
362 |
|
---|
363 | <a href="qlabel.html">QLabel</a> *liAddress = new <a href="qlabel.html">QLabel</a>( "Add&ress", input );
|
---|
364 | liAddress-><a href="qwidget.html#resize">resize</a>( liAddress-><a href="qwidget.html#sizeHint">sizeHint</a>() );
|
---|
365 | grid1-><a href="qgridlayout.html#addWidget">addWidget</a>( liAddress, 0, 2 );
|
---|
366 |
|
---|
367 | <a href="qlabel.html">QLabel</a> *liEMail = new <a href="qlabel.html">QLabel</a>( "&E-Mail", input );
|
---|
368 | liEMail-><a href="qwidget.html#resize">resize</a>( liEMail-><a href="qwidget.html#sizeHint">sizeHint</a>() );
|
---|
369 | grid1-><a href="qgridlayout.html#addWidget">addWidget</a>( liEMail, 0, 3 );
|
---|
370 |
|
---|
371 | add = new <a href="qpushbutton.html">QPushButton</a>( "A&dd", input );
|
---|
372 | <a name="x599"></a><a name="x598"></a> add-><a href="qwidget.html#resize">resize</a>( add-><a href="qwidget.html#sizeHint">sizeHint</a>() );
|
---|
373 | grid1-><a href="qgridlayout.html#addWidget">addWidget</a>( add, 0, 4 );
|
---|
374 | <a href="qobject.html#connect">connect</a>( add, SIGNAL( <a href="qbutton.html#clicked">clicked</a>() ), this, SLOT( addEntry() ) );
|
---|
375 |
|
---|
376 | iFirstName = new <a href="qlineedit.html">QLineEdit</a>( input );
|
---|
377 | <a name="x583"></a> iFirstName-><a href="qwidget.html#resize">resize</a>( iFirstName-><a href="qlineedit.html#sizeHint">sizeHint</a>() );
|
---|
378 | grid1-><a href="qgridlayout.html#addWidget">addWidget</a>( iFirstName, 1, 0 );
|
---|
379 | <a name="x580"></a> liFirstName-><a href="qlabel.html#setBuddy">setBuddy</a>( iFirstName );
|
---|
380 |
|
---|
381 | iLastName = new <a href="qlineedit.html">QLineEdit</a>( input );
|
---|
382 | iLastName-><a href="qwidget.html#resize">resize</a>( iLastName-><a href="qlineedit.html#sizeHint">sizeHint</a>() );
|
---|
383 | grid1-><a href="qgridlayout.html#addWidget">addWidget</a>( iLastName, 1, 1 );
|
---|
384 | liLastName-><a href="qlabel.html#setBuddy">setBuddy</a>( iLastName );
|
---|
385 |
|
---|
386 | iAddress = new <a href="qlineedit.html">QLineEdit</a>( input );
|
---|
387 | iAddress-><a href="qwidget.html#resize">resize</a>( iAddress-><a href="qlineedit.html#sizeHint">sizeHint</a>() );
|
---|
388 | grid1-><a href="qgridlayout.html#addWidget">addWidget</a>( iAddress, 1, 2 );
|
---|
389 | liAddress-><a href="qlabel.html#setBuddy">setBuddy</a>( iAddress );
|
---|
390 |
|
---|
391 | iEMail = new <a href="qlineedit.html">QLineEdit</a>( input );
|
---|
392 | iEMail-><a href="qwidget.html#resize">resize</a>( iEMail-><a href="qlineedit.html#sizeHint">sizeHint</a>() );
|
---|
393 | grid1-><a href="qgridlayout.html#addWidget">addWidget</a>( iEMail, 1, 3 );
|
---|
394 | liEMail-><a href="qlabel.html#setBuddy">setBuddy</a>( iEMail );
|
---|
395 |
|
---|
396 | change = new <a href="qpushbutton.html">QPushButton</a>( "&Change", input );
|
---|
397 | change-><a href="qwidget.html#resize">resize</a>( change-><a href="qwidget.html#sizeHint">sizeHint</a>() );
|
---|
398 | grid1-><a href="qgridlayout.html#addWidget">addWidget</a>( change, 1, 4 );
|
---|
399 | <a href="qobject.html#connect">connect</a>( change, SIGNAL( <a href="qbutton.html#clicked">clicked</a>() ), this, SLOT( changeEntry() ) );
|
---|
400 |
|
---|
401 | <a name="x600"></a> tabWidget-><a href="qtabwidget.html#addTab">addTab</a>( input, "&Add/Change Entry" );
|
---|
402 |
|
---|
403 | // --------------------------------------
|
---|
404 |
|
---|
405 | <a href="qwidget.html">QWidget</a> *search = new <a href="qwidget.html">QWidget</a>( this );
|
---|
406 | <a href="qgridlayout.html">QGridLayout</a> *grid2 = new <a href="qgridlayout.html">QGridLayout</a>( search, 2, 5, 5, 5 );
|
---|
407 |
|
---|
408 | cFirstName = new <a href="qcheckbox.html">QCheckBox</a>( "First &Name", search );
|
---|
409 | <a name="x575"></a> cFirstName-><a href="qwidget.html#resize">resize</a>( cFirstName-><a href="qwidget.html#sizeHint">sizeHint</a>() );
|
---|
410 | grid2-><a href="qgridlayout.html#addWidget">addWidget</a>( cFirstName, 0, 0 );
|
---|
411 | <a href="qobject.html#connect">connect</a>( cFirstName, SIGNAL( <a href="qbutton.html#clicked">clicked</a>() ), this, SLOT( toggleFirstName() ) );
|
---|
412 |
|
---|
413 | cLastName = new <a href="qcheckbox.html">QCheckBox</a>( "&Last Name", search );
|
---|
414 | cLastName-><a href="qwidget.html#resize">resize</a>( cLastName-><a href="qwidget.html#sizeHint">sizeHint</a>() );
|
---|
415 | grid2-><a href="qgridlayout.html#addWidget">addWidget</a>( cLastName, 0, 1 );
|
---|
416 | <a href="qobject.html#connect">connect</a>( cLastName, SIGNAL( <a href="qbutton.html#clicked">clicked</a>() ), this, SLOT( toggleLastName() ) );
|
---|
417 |
|
---|
418 | cAddress = new <a href="qcheckbox.html">QCheckBox</a>( "Add&ress", search );
|
---|
419 | cAddress-><a href="qwidget.html#resize">resize</a>( cAddress-><a href="qwidget.html#sizeHint">sizeHint</a>() );
|
---|
420 | grid2-><a href="qgridlayout.html#addWidget">addWidget</a>( cAddress, 0, 2 );
|
---|
421 | <a href="qobject.html#connect">connect</a>( cAddress, SIGNAL( <a href="qbutton.html#clicked">clicked</a>() ), this, SLOT( toggleAddress() ) );
|
---|
422 |
|
---|
423 | cEMail = new <a href="qcheckbox.html">QCheckBox</a>( "&E-Mail", search );
|
---|
424 | cEMail-><a href="qwidget.html#resize">resize</a>( cEMail-><a href="qwidget.html#sizeHint">sizeHint</a>() );
|
---|
425 | grid2-><a href="qgridlayout.html#addWidget">addWidget</a>( cEMail, 0, 3 );
|
---|
426 | <a href="qobject.html#connect">connect</a>( cEMail, SIGNAL( <a href="qbutton.html#clicked">clicked</a>() ), this, SLOT( toggleEMail() ) );
|
---|
427 |
|
---|
428 | sFirstName = new <a href="qlineedit.html">QLineEdit</a>( search );
|
---|
429 | sFirstName-><a href="qwidget.html#resize">resize</a>( sFirstName-><a href="qlineedit.html#sizeHint">sizeHint</a>() );
|
---|
430 | grid2-><a href="qgridlayout.html#addWidget">addWidget</a>( sFirstName, 1, 0 );
|
---|
431 |
|
---|
432 | sLastName = new <a href="qlineedit.html">QLineEdit</a>( search );
|
---|
433 | sLastName-><a href="qwidget.html#resize">resize</a>( sLastName-><a href="qlineedit.html#sizeHint">sizeHint</a>() );
|
---|
434 | grid2-><a href="qgridlayout.html#addWidget">addWidget</a>( sLastName, 1, 1 );
|
---|
435 |
|
---|
436 | sAddress = new <a href="qlineedit.html">QLineEdit</a>( search );
|
---|
437 | sAddress-><a href="qwidget.html#resize">resize</a>( sAddress-><a href="qlineedit.html#sizeHint">sizeHint</a>() );
|
---|
438 | grid2-><a href="qgridlayout.html#addWidget">addWidget</a>( sAddress, 1, 2 );
|
---|
439 |
|
---|
440 | sEMail = new <a href="qlineedit.html">QLineEdit</a>( search );
|
---|
441 | sEMail-><a href="qwidget.html#resize">resize</a>( sEMail-><a href="qlineedit.html#sizeHint">sizeHint</a>() );
|
---|
442 | grid2-><a href="qgridlayout.html#addWidget">addWidget</a>( sEMail, 1, 3 );
|
---|
443 |
|
---|
444 | find = new <a href="qpushbutton.html">QPushButton</a>( "F&ind", search );
|
---|
445 | find-><a href="qwidget.html#resize">resize</a>( find-><a href="qwidget.html#sizeHint">sizeHint</a>() );
|
---|
446 | grid2-><a href="qgridlayout.html#addWidget">addWidget</a>( find, 1, 4 );
|
---|
447 | <a href="qobject.html#connect">connect</a>( find, SIGNAL( <a href="qbutton.html#clicked">clicked</a>() ), this, SLOT( findEntries() ) );
|
---|
448 |
|
---|
449 | <a name="x574"></a> cFirstName-><a href="qcheckbox.html#setChecked">setChecked</a>( TRUE );
|
---|
450 | <a name="x605"></a> sFirstName-><a href="qwidget.html#setEnabled">setEnabled</a>( TRUE );
|
---|
451 | sLastName-><a href="qwidget.html#setEnabled">setEnabled</a>( FALSE );
|
---|
452 | sAddress-><a href="qwidget.html#setEnabled">setEnabled</a>( FALSE );
|
---|
453 | sEMail-><a href="qwidget.html#setEnabled">setEnabled</a>( FALSE );
|
---|
454 |
|
---|
455 | tabWidget-><a href="qtabwidget.html#addTab">addTab</a>( search, "&Search" );
|
---|
456 |
|
---|
457 | mainGrid-><a href="qgridlayout.html#addWidget">addWidget</a>( tabWidget, 0, 0 );
|
---|
458 | }
|
---|
459 |
|
---|
460 | void <a name="f278"></a>ABCentralWidget::setupListView()
|
---|
461 | {
|
---|
462 | listView = new <a href="qlistview.html">QListView</a>( this );
|
---|
463 | <a name="x585"></a> listView-><a href="qlistview.html#addColumn">addColumn</a>( "First Name" );
|
---|
464 | listView-><a href="qlistview.html#addColumn">addColumn</a>( "Last Name" );
|
---|
465 | listView-><a href="qlistview.html#addColumn">addColumn</a>( "Address" );
|
---|
466 | listView-><a href="qlistview.html#addColumn">addColumn</a>( "E-Mail" );
|
---|
467 |
|
---|
468 | <a name="x592"></a> listView-><a href="qlistview.html#setSelectionMode">setSelectionMode</a>( QListView::Single );
|
---|
469 |
|
---|
470 | <a name="x588"></a> <a href="qobject.html#connect">connect</a>( listView, SIGNAL( <a href="qlistview.html#clicked">clicked</a>( <a href="qlistviewitem.html">QListViewItem</a>* ) ), this, SLOT( itemSelected( <a href="qlistviewitem.html">QListViewItem</a>* ) ) );
|
---|
471 |
|
---|
472 | mainGrid-><a href="qgridlayout.html#addWidget">addWidget</a>( listView, 1, 0 );
|
---|
473 | <a name="x591"></a> listView-><a href="qlistview.html#setAllColumnsShowFocus">setAllColumnsShowFocus</a>( TRUE );
|
---|
474 | }
|
---|
475 |
|
---|
476 | void <a name="f279"></a>ABCentralWidget::addEntry()
|
---|
477 | {
|
---|
478 | <a name="x584"></a> if ( !iFirstName-><a href="qlineedit.html#text">text</a>().isEmpty() || !iLastName-><a href="qlineedit.html#text">text</a>().isEmpty() ||
|
---|
479 | !iAddress-><a href="qlineedit.html#text">text</a>().isEmpty() || !iEMail-><a href="qlineedit.html#text">text</a>().isEmpty() ) {
|
---|
480 | <a href="qlistviewitem.html">QListViewItem</a> *item = new <a href="qlistviewitem.html">QListViewItem</a>( listView );
|
---|
481 | item-><a href="qlistviewitem.html#setText">setText</a>( 0, iFirstName-><a href="qlineedit.html#text">text</a>() );
|
---|
482 | item-><a href="qlistviewitem.html#setText">setText</a>( 1, iLastName-><a href="qlineedit.html#text">text</a>() );
|
---|
483 | item-><a href="qlistviewitem.html#setText">setText</a>( 2, iAddress-><a href="qlineedit.html#text">text</a>() );
|
---|
484 | item-><a href="qlistviewitem.html#setText">setText</a>( 3, iEMail-><a href="qlineedit.html#text">text</a>() );
|
---|
485 | }
|
---|
486 |
|
---|
487 | <a name="x582"></a> iFirstName-><a href="qlineedit.html#setText">setText</a>( "" );
|
---|
488 | iLastName-><a href="qlineedit.html#setText">setText</a>( "" );
|
---|
489 | iAddress-><a href="qlineedit.html#setText">setText</a>( "" );
|
---|
490 | iEMail-><a href="qlineedit.html#setText">setText</a>( "" );
|
---|
491 | }
|
---|
492 |
|
---|
493 | void <a name="f280"></a>ABCentralWidget::changeEntry()
|
---|
494 | {
|
---|
495 | <a name="x589"></a> <a href="qlistviewitem.html">QListViewItem</a> *item = listView-><a href="qlistview.html#currentItem">currentItem</a>();
|
---|
496 |
|
---|
497 | if ( item &&
|
---|
498 | ( !iFirstName-><a href="qlineedit.html#text">text</a>().isEmpty() || !iLastName-><a href="qlineedit.html#text">text</a>().isEmpty() ||
|
---|
499 | !iAddress-><a href="qlineedit.html#text">text</a>().isEmpty() || !iEMail-><a href="qlineedit.html#text">text</a>().isEmpty() ) ) {
|
---|
500 | item-><a href="qlistviewitem.html#setText">setText</a>( 0, iFirstName-><a href="qlineedit.html#text">text</a>() );
|
---|
501 | item-><a href="qlistviewitem.html#setText">setText</a>( 1, iLastName-><a href="qlineedit.html#text">text</a>() );
|
---|
502 | item-><a href="qlistviewitem.html#setText">setText</a>( 2, iAddress-><a href="qlineedit.html#text">text</a>() );
|
---|
503 | item-><a href="qlistviewitem.html#setText">setText</a>( 3, iEMail-><a href="qlineedit.html#text">text</a>() );
|
---|
504 | }
|
---|
505 | }
|
---|
506 |
|
---|
507 | void <a name="f281"></a>ABCentralWidget::selectionChanged()
|
---|
508 | {
|
---|
509 | iFirstName-><a href="qlineedit.html#setText">setText</a>( "" );
|
---|
510 | iLastName-><a href="qlineedit.html#setText">setText</a>( "" );
|
---|
511 | iAddress-><a href="qlineedit.html#setText">setText</a>( "" );
|
---|
512 | iEMail-><a href="qlineedit.html#setText">setText</a>( "" );
|
---|
513 | }
|
---|
514 |
|
---|
515 | void <a name="f282"></a>ABCentralWidget::itemSelected( <a href="qlistviewitem.html">QListViewItem</a> *item )
|
---|
516 | {
|
---|
517 | if ( !item )
|
---|
518 | return;
|
---|
519 | <a name="x594"></a> item-><a href="qlistviewitem.html#setSelected">setSelected</a>( TRUE );
|
---|
520 | <a name="x593"></a> item-><a href="qlistviewitem.html#repaint">repaint</a>();
|
---|
521 |
|
---|
522 | <a name="x596"></a> iFirstName-><a href="qlineedit.html#setText">setText</a>( item-><a href="qlistviewitem.html#text">text</a>( 0 ) );
|
---|
523 | iLastName-><a href="qlineedit.html#setText">setText</a>( item-><a href="qlistviewitem.html#text">text</a>( 1 ) );
|
---|
524 | iAddress-><a href="qlineedit.html#setText">setText</a>( item-><a href="qlistviewitem.html#text">text</a>( 2 ) );
|
---|
525 | iEMail-><a href="qlineedit.html#setText">setText</a>( item-><a href="qlistviewitem.html#text">text</a>( 3 ) );
|
---|
526 | }
|
---|
527 |
|
---|
528 | void <a name="f283"></a>ABCentralWidget::toggleFirstName()
|
---|
529 | {
|
---|
530 | sFirstName-><a href="qlineedit.html#setText">setText</a>( "" );
|
---|
531 |
|
---|
532 | <a name="x573"></a> if ( cFirstName-><a href="qcheckbox.html#isChecked">isChecked</a>() ) {
|
---|
533 | sFirstName-><a href="qwidget.html#setEnabled">setEnabled</a>( TRUE );
|
---|
534 | <a name="x606"></a> sFirstName-><a href="qwidget.html#setFocus">setFocus</a>();
|
---|
535 | }
|
---|
536 | else
|
---|
537 | sFirstName-><a href="qwidget.html#setEnabled">setEnabled</a>( FALSE );
|
---|
538 | }
|
---|
539 |
|
---|
540 | void <a name="f284"></a>ABCentralWidget::toggleLastName()
|
---|
541 | {
|
---|
542 | sLastName-><a href="qlineedit.html#setText">setText</a>( "" );
|
---|
543 |
|
---|
544 | if ( cLastName-><a href="qcheckbox.html#isChecked">isChecked</a>() ) {
|
---|
545 | sLastName-><a href="qwidget.html#setEnabled">setEnabled</a>( TRUE );
|
---|
546 | sLastName-><a href="qwidget.html#setFocus">setFocus</a>();
|
---|
547 | }
|
---|
548 | else
|
---|
549 | sLastName-><a href="qwidget.html#setEnabled">setEnabled</a>( FALSE );
|
---|
550 | }
|
---|
551 |
|
---|
552 | void <a name="f285"></a>ABCentralWidget::toggleAddress()
|
---|
553 | {
|
---|
554 | sAddress-><a href="qlineedit.html#setText">setText</a>( "" );
|
---|
555 |
|
---|
556 | if ( cAddress-><a href="qcheckbox.html#isChecked">isChecked</a>() ) {
|
---|
557 | sAddress-><a href="qwidget.html#setEnabled">setEnabled</a>( TRUE );
|
---|
558 | sAddress-><a href="qwidget.html#setFocus">setFocus</a>();
|
---|
559 | }
|
---|
560 | else
|
---|
561 | sAddress-><a href="qwidget.html#setEnabled">setEnabled</a>( FALSE );
|
---|
562 | }
|
---|
563 |
|
---|
564 | void <a name="f286"></a>ABCentralWidget::toggleEMail()
|
---|
565 | {
|
---|
566 | sEMail-><a href="qlineedit.html#setText">setText</a>( "" );
|
---|
567 |
|
---|
568 | if ( cEMail-><a href="qcheckbox.html#isChecked">isChecked</a>() ) {
|
---|
569 | sEMail-><a href="qwidget.html#setEnabled">setEnabled</a>( TRUE );
|
---|
570 | sEMail-><a href="qwidget.html#setFocus">setFocus</a>();
|
---|
571 | }
|
---|
572 | else
|
---|
573 | sEMail-><a href="qwidget.html#setEnabled">setEnabled</a>( FALSE );
|
---|
574 | }
|
---|
575 |
|
---|
576 | void <a name="f287"></a>ABCentralWidget::findEntries()
|
---|
577 | {
|
---|
578 | if ( !cFirstName-><a href="qcheckbox.html#isChecked">isChecked</a>() &&
|
---|
579 | !cLastName-><a href="qcheckbox.html#isChecked">isChecked</a>() &&
|
---|
580 | !cAddress-><a href="qcheckbox.html#isChecked">isChecked</a>() &&
|
---|
581 | !cEMail-><a href="qcheckbox.html#isChecked">isChecked</a>() ) {
|
---|
582 | <a name="x587"></a> listView-><a href="qlistview.html#clearSelection">clearSelection</a>();
|
---|
583 | return;
|
---|
584 | }
|
---|
585 |
|
---|
586 | <a href="qlistviewitemiterator.html">QListViewItemIterator</a> it( listView );
|
---|
587 |
|
---|
588 | for ( ; it.<a href="qlistviewitemiterator.html#current">current</a>(); ++it ) {
|
---|
589 | bool select = TRUE;
|
---|
590 |
|
---|
591 | if ( cFirstName-><a href="qcheckbox.html#isChecked">isChecked</a>() ) {
|
---|
592 | if ( select && it.<a href="qlistviewitemiterator.html#current">current</a>()->text( 0 ).contains( sFirstName-><a href="qlineedit.html#text">text</a>() ) )
|
---|
593 | select = TRUE;
|
---|
594 | else
|
---|
595 | select = FALSE;
|
---|
596 | }
|
---|
597 | if ( cLastName-><a href="qcheckbox.html#isChecked">isChecked</a>() ) {
|
---|
598 | if ( select && it.<a href="qlistviewitemiterator.html#current">current</a>()->text( 1 ).contains( sLastName-><a href="qlineedit.html#text">text</a>() ) )
|
---|
599 | select = TRUE;
|
---|
600 | else
|
---|
601 | select = FALSE;
|
---|
602 | }
|
---|
603 | if ( cAddress-><a href="qcheckbox.html#isChecked">isChecked</a>() ) {
|
---|
604 | if ( select && it.<a href="qlistviewitemiterator.html#current">current</a>()->text( 2 ).contains( sAddress-><a href="qlineedit.html#text">text</a>() ) )
|
---|
605 | select = TRUE;
|
---|
606 | else
|
---|
607 | select = FALSE;
|
---|
608 | }
|
---|
609 | if ( cEMail-><a href="qcheckbox.html#isChecked">isChecked</a>() ) {
|
---|
610 | if ( select && it.<a href="qlistviewitemiterator.html#current">current</a>()->text( 3 ).contains( sEMail-><a href="qlineedit.html#text">text</a>() ) )
|
---|
611 | select = TRUE;
|
---|
612 | else
|
---|
613 | select = FALSE;
|
---|
614 | }
|
---|
615 |
|
---|
616 | if ( select )
|
---|
617 | it.<a href="qlistviewitemiterator.html#current">current</a>()->setSelected( TRUE );
|
---|
618 | else
|
---|
619 | it.<a href="qlistviewitemiterator.html#current">current</a>()->setSelected( FALSE );
|
---|
620 | it.<a href="qlistviewitemiterator.html#current">current</a>()->repaint();
|
---|
621 | }
|
---|
622 | }
|
---|
623 | </pre>
|
---|
624 |
|
---|
625 | <p> <hr>
|
---|
626 | <p> Main:
|
---|
627 | <p> <pre>/****************************************************************************
|
---|
628 | ** $Id: addressbook-example.html 2051 2007-02-21 10:04:20Z chehrlic $
|
---|
629 | **
|
---|
630 | ** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.
|
---|
631 | **
|
---|
632 | ** This file is part of an example program for Qt. This example
|
---|
633 | ** program may be used, distributed and modified without limitation.
|
---|
634 | **
|
---|
635 | *****************************************************************************/
|
---|
636 |
|
---|
637 | #include <<a href="qapplication-h.html">qapplication.h</a>>
|
---|
638 |
|
---|
639 | #include "mainwindow.h"
|
---|
640 |
|
---|
641 | int main( int argc, char ** argv )
|
---|
642 | {
|
---|
643 | <a href="qapplication.html">QApplication</a> a( argc, argv );
|
---|
644 |
|
---|
645 | ABMainWindow *mw = new ABMainWindow();
|
---|
646 | mw-><a href="qwidget.html#setCaption">setCaption</a>( "Qt Example - Addressbook" );
|
---|
647 | a.<a href="qapplication.html#setMainWidget">setMainWidget</a>( mw );
|
---|
648 | <a name="x611"></a> mw-><a href="qwidget.html#show">show</a>();
|
---|
649 |
|
---|
650 | <a name="x609"></a><a name="x608"></a> a.<a href="qobject.html#connect">connect</a>( &a, SIGNAL( <a href="qapplication.html#lastWindowClosed">lastWindowClosed</a>() ), &a, SLOT( <a href="qapplication.html#quit">quit</a>() ) );
|
---|
651 | int result = a.<a href="qapplication.html#exec">exec</a>();
|
---|
652 | delete mw;
|
---|
653 | return result;
|
---|
654 | }
|
---|
655 | </pre>
|
---|
656 |
|
---|
657 | <p>See also <a href="examples.html">Examples</a>.
|
---|
658 |
|
---|
659 | <!-- eof -->
|
---|
660 | <p><address><hr><div align=center>
|
---|
661 | <table width=100% cellspacing=0 border=0><tr>
|
---|
662 | <td>Copyright © 2007
|
---|
663 | <a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
|
---|
664 | <td align=right><div align=right>Qt 3.3.8</div>
|
---|
665 | </table></div></address></body>
|
---|
666 | </html>
|
---|