📄 addressbook-main-cpp.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Qt Toolkit - addressbook/main.cpp example file</title><style type="text/css"><!--h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm; }a:link { color: #004faf; text-decoration: none }a:visited { color: #672967; text-decoration: none }body { background: white; color: black; }--></style></head><body bgcolor="#ffffff"><table width="100%"><tr><td><a href="index.html"><img width="100" height="100" src="qtlogo.png"alt="Home" border="0"><img width="100"height="100" src="face.png" alt="Home" border="0"></a><td valign="top"><div align="right"><img src="dochead.png" width="472" height="27"><br><a href="classes.html"><b>Classes</b></a>- <a href="annotated.html">Annotated</a>- <a href="hierarchy.html">Tree</a>- <a href="functions.html">Functions</a>- <a href="index.html">Home</a>- <a href="topicals.html"><b>Structure</b> <font face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular" align="center" size=32>Qte</font></a></div></table><h1 align=center>Simple Addressbook</h1><br clear="all"> This examples shows how to write a very simple, but complete application using a very simple addressbook as example. <hr> Header file of the mainwindow: <pre>/****************************************************************************** $Id: qt/examples/addressbook/mainwindow.h 2.3.8 edited 2004-05-12 $**** Copyright ( C ) 1992-2000 Trolltech AS. All rights reserved.**** This file is part of an example program for Qt. This example** program may be used, distributed and modified without limitation.*******************************************************************************/#ifndef AB_MAINWINDOW_H#define AB_MAINWINDOW_H#include <<a href="qmainwindow-h.html">qmainwindow.h</a>>#include <<a href="qstring-h.html">qstring.h</a>>class QToolBar;class QPopupMenu;class ABCentralWidget;class ABMainWindow: public QMainWindow{ Q_OBJECTpublic: ABMainWindow(); ~ABMainWindow();protected slots: void fileNew(); void fileOpen(); void fileSave(); void fileSaveAs(); void filePrint(); void closeWindow();protected: void setupMenuBar(); void setupFileTools(); void setupStatusBar(); void setupCentralWidget(); <a href="qtoolbar.html">QToolBar</a> *fileTools; <a href="qstring.html">QString</a> filename; ABCentralWidget *view;};#endif</pre> <hr> Implementation of the mainwindow: <pre>/****************************************************************************** $Id: qt/examples/addressbook/mainwindow.cpp 2.3.8 edited 2004-05-12 $**** Copyright ( C ) 1992-2000 Trolltech AS. All rights reserved.**** This file is part of an example program for Qt. This example** program may be used, distributed and modified without limitation.*******************************************************************************/#include "mainwindow.h"#include "centralwidget.h"#include <<a href="qtoolbar-h.html">qtoolbar.h</a>>#include <<a href="qtoolbutton-h.html">qtoolbutton.h</a>>#include <<a href="qpopupmenu-h.html">qpopupmenu.h</a>>#include <<a href="qmenubar-h.html">qmenubar.h</a>>#include <qkeycode.h>#include <<a href="qstatusbar-h.html">qstatusbar.h</a>>#include <<a href="qapplication-h.html">qapplication.h</a>>#include <<a href="qfiledialog-h.html">qfiledialog.h</a>>ABMainWindow::ABMainWindow() : <a href="qmainwindow.html">QMainWindow</a>( 0, "example addressbook application" ), filename( QString::null ){ <a href=#237>setupMenuBar</a>(); <a href=#238>setupFileTools</a>(); <a href=#239>setupStatusBar</a>(); <a href=#240>setupCentralWidget</a>();}ABMainWindow::~ABMainWindow(){}void <a name="237"></a>ABMainWindow::setupMenuBar(){ <a href="qpopupmenu.html">QPopupMenu</a> *file = new <a href="qpopupmenu.html">QPopupMenu</a>( this ); <a href="qmainwindow.html#0eb7bc">menuBar</a>()->insertItem( "&File", file ); file-><a href="qmenudata.html#deddb9">insertItem</a>( "New", this, SLOT( <a href=#242>fileNew</a>() ), CTRL + Key_N ); file-><a href="qmenudata.html#deddb9">insertItem</a>( <a href="qpixmap.html">QPixmap</a>( "fileopen.xpm" ), "Open", this, SLOT( <a href=#243>fileOpen</a>() ), CTRL + Key_O ); file-><a href="qmenudata.html#e34b79">insertSeparator</a>(); file-><a href="qmenudata.html#deddb9">insertItem</a>( <a href="qpixmap.html">QPixmap</a>( "filesave.xpm" ), "Save", this, SLOT( <a href=#244>fileSave</a>() ), CTRL + Key_S ); file-><a href="qmenudata.html#deddb9">insertItem</a>( "Save As...", this, SLOT( <a href=#245>fileSaveAs</a>() ) ); file-><a href="qmenudata.html#e34b79">insertSeparator</a>(); file-><a href="qmenudata.html#deddb9">insertItem</a>( <a href="qpixmap.html">QPixmap</a>( "fileprint.xpm" ), "Print...", this, SLOT( <a href=#246>filePrint</a>() ), CTRL + Key_P ); file-><a href="qmenudata.html#e34b79">insertSeparator</a>(); file-><a href="qmenudata.html#deddb9">insertItem</a>( "Close", this, SLOT( <a href=#241>closeWindow</a>() ), CTRL + Key_W ); file-><a href="qmenudata.html#deddb9">insertItem</a>( "Quit", qApp, SLOT( quit() ), CTRL + Key_Q );}void <a name="238"></a>ABMainWindow::setupFileTools(){ //fileTools = new <a href="qtoolbar.html">QToolBar</a>( this, "file operations" );}void <a name="239"></a>ABMainWindow::setupStatusBar(){ //statusBar()->message( "Ready", 2000 );}void <a name="240"></a>ABMainWindow::setupCentralWidget(){ view = new ABCentralWidget( this ); <a href="qmainwindow.html#fce9ba">setCentralWidget</a>( view );}void <a name="241"></a>ABMainWindow::closeWindow(){ <a href="qwidget.html#3d9c87">close</a>();}void <a name="242"></a>ABMainWindow::fileNew(){}void <a name="243"></a>ABMainWindow::fileOpen(){ <a href="qstring.html">QString</a> fn = QFileDialog::getOpenFileName( QString::null, QString::null, this ); if ( !fn.<a href="qstring.html#c62623">isEmpty</a>() ) { filename = fn; view->load( filename ); }}void <a name="244"></a>ABMainWindow::fileSave(){ if ( filename.isEmpty() ) { <a href=#245>fileSaveAs</a>(); return; } view->save( filename );}void <a name="245"></a>ABMainWindow::fileSaveAs(){ <a href="qstring.html">QString</a> fn = QFileDialog::getSaveFileName( QString::null, QString::null, this ); if ( !fn.<a href="qstring.html#c62623">isEmpty</a>() ) { filename = fn; <a href=#244>fileSave</a>(); }}void <a name="246"></a>ABMainWindow::filePrint(){}</pre> <hr> Header file of the centralwidget: <pre>/****************************************************************************** $Id: qt/examples/addressbook/centralwidget.h 2.3.8 edited 2004-05-12 $**** Copyright ( C ) 1992-2000 Trolltech AS. All rights reserved.**** This file is part of an example program for Qt. This example** program may be used, distributed and modified without limitation.*******************************************************************************/#ifndef AB_CENTRALWIDGET_H#define AB_CENTRALWIDGET_H#include <<a href="qwidget-h.html">qwidget.h</a>>#include <<a href="qstring-h.html">qstring.h</a>>class QTabWidget;class QListView;class QGridLayout;class QLineEdit;class QPushButton;class QListViewItem;class QCheckBox;class ABCentralWidget : public QWidget{ Q_OBJECTpublic: ABCentralWidget( <a href="qwidget.html">QWidget</a> *parent, const char *name = 0 ); void save( const QString &filename ); void load( const QString &filename );protected slots: void addEntry(); void changeEntry(); void itemSelected( <a href="qlistviewitem.html">QListViewItem</a>* ); void selectionChanged(); void toggleFirstName(); void toggleLastName(); void toggleAddress(); void toggleEMail(); void findEntries();protected: void setupTabWidget(); void setupListView(); <a href="qgridlayout.html">QGridLayout</a> *mainGrid; <a href="qtabwidget.html">QTabWidget</a> *tabWidget; <a href="qlistview.html">QListView</a> *listView; <a href="qpushbutton.html">QPushButton</a> *add, *change, *find; <a href="qlineedit.html">QLineEdit</a> *iFirstName, *iLastName, *iAddress, *iEMail, *sFirstName, *sLastName, *sAddress, *sEMail; <a href="qcheckbox.html">QCheckBox</a> *cFirstName, *cLastName, *cAddress, *cEMail;};#endif</pre> <hr> Implementation of the centralwidget: <pre>/****************************************************************************** $Id: qt/examples/addressbook/centralwidget.cpp 2.3.8 edited 2004-05-12 $**** Copyright ( C ) 1992-2000 Trolltech AS. All rights reserved.**** This file is part of an example program for Qt. This example** program may be used, distributed and modified without limitation.*******************************************************************************/#include "centralwidget.h"#include <<a href="qtabwidget-h.html">qtabwidget.h</a>>#include <<a href="qlistview-h.html">qlistview.h</a>>#include <<a href="qlayout-h.html">qlayout.h</a>>#include <<a href="qwidget-h.html">qwidget.h</a>>#include <<a href="qlabel-h.html">qlabel.h</a>>#include <<a href="qpushbutton-h.html">qpushbutton.h</a>>#include <<a href="qlineedit-h.html">qlineedit.h</a>>#include <<a href="qlabel-h.html">qlabel.h</a>>#include <<a href="qcheckbox-h.html">qcheckbox.h</a>>#include <<a href="qfile-h.html">qfile.h</a>>#include <<a href="qtextstream-h.html">qtextstream.h</a>>ABCentralWidget::ABCentralWidget( <a href="qwidget.html">QWidget</a> *parent, const char *name ) : <a href="qwidget.html">QWidget</a>( parent, name ){ mainGrid = new <a href="qgridlayout.html">QGridLayout</a>( this, 2, 1, 5, 5 ); <a href=#249>setupTabWidget</a>(); <a href=#250>setupListView</a>(); mainGrid->setRowStretch( 0, 0 ); mainGrid->setRowStretch( 1, 1 );}void <a name="247"></a>ABCentralWidget::save( const QString &filename ){ if ( !listView->firstChild() ) return; <a href="qfile.html">QFile</a> f( filename ); if ( !f.<a href="qfile.html#255995">open</a>( IO_WriteOnly ) ) return; <a href="qtextstream.html">QTextStream</a> t( &f ); <a href="qlistviewitemiterator.html">QListViewItemIterator</a> it( listView ); for ( ; it.<a href="qlistviewitemiterator.html#b134cd">current</a>(); ++it ) for ( unsigned int i = 0; i < 4; i++ ) t << it.<a href="qlistviewitemiterator.html#b134cd">current</a>()->text( i ) << "\n"; f.<a href="qfile.html#64e640">close</a>();}void <a name="248"></a>ABCentralWidget::load( const QString &filename ){ listView->clear(); <a href="qfile.html">QFile</a> f( filename );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -