[190] | 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/doc/tutorial2.doc:969 -->
|
---|
| 3 | <html>
|
---|
| 4 | <head>
|
---|
| 5 | <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
---|
| 6 | <title>File Handling</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>File Handling</h1>
|
---|
| 33 |
|
---|
| 34 |
|
---|
| 35 | <p>
|
---|
| 36 | <p> (Extracts from <tt>chartform_files.cpp</tt>.)
|
---|
| 37 | <p> <h2> Reading Chart Data
|
---|
| 38 | </h2>
|
---|
| 39 | <a name="1"></a><p>
|
---|
| 40 |
|
---|
| 41 | <pre> void ChartForm::load( const <a href="qstring.html">QString</a>& filename )
|
---|
| 42 | {
|
---|
| 43 | <a href="qfile.html">QFile</a> file( filename );
|
---|
| 44 | if ( !file.<a href="qfile.html#open">open</a>( <a href="qfile.html#open">IO_ReadOnly</a> ) ) {
|
---|
| 45 | <a href="qmainwindow.html#statusBar">statusBar</a>()->message( QString( "Failed to load \'%1\'" ).
|
---|
| 46 | arg( filename ), 2000 );
|
---|
| 47 | return;
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | init(); // Make sure we have colours
|
---|
| 51 | m_filename = filename;
|
---|
| 52 | <a href="qtextstream.html">QTextStream</a> ts( &file );
|
---|
| 53 | Element element;
|
---|
| 54 | int errors = 0;
|
---|
| 55 | int i = 0;
|
---|
| 56 | <a name="x2592"></a> while ( !ts.<a href="qtextstream.html#eof">eof</a>() ) {
|
---|
| 57 | ts >> element;
|
---|
| 58 | if ( element.isValid() )
|
---|
| 59 | m_elements[i++] = element;
|
---|
| 60 | </pre><pre> <a name="x2590"></a> file.<a href="qfile.html#close">close</a>();
|
---|
| 61 | </pre>
|
---|
| 62 | <p> <pre> <a href="qwidget.html#setCaption">setCaption</a>( QString( "Chart -- %1" ).arg( filename ) );
|
---|
| 63 | updateRecentFiles( filename );
|
---|
| 64 |
|
---|
| 65 | drawElements();
|
---|
| 66 | m_changed = FALSE;
|
---|
| 67 | }
|
---|
| 68 | </pre>
|
---|
| 69 | <p> Loading a data set is very easy. We open the file and create a text
|
---|
| 70 | stream. While there's data to read we stream an element into <tt>element</tt> and if it is valid we insert it into the <tt>m_elements</tt> vector.
|
---|
| 71 | All the detail is handled by the <tt>Element</tt> class. Then we close
|
---|
| 72 | the file and update the caption and the recent files list. Finally we
|
---|
| 73 | draw the chart and mark it as unchanged.
|
---|
| 74 | <p> <h2> Writing Chart Data
|
---|
| 75 | </h2>
|
---|
| 76 | <a name="2"></a><p> <pre> void ChartForm::fileSave()
|
---|
| 77 | {
|
---|
| 78 | </pre><pre> <a href="qfile.html">QFile</a> file( m_filename );
|
---|
| 79 | <a name="x2591"></a> if ( !file.<a href="qfile.html#open">open</a>( <a href="qfile.html#open">IO_WriteOnly</a> ) ) {
|
---|
| 80 | <a href="qmainwindow.html#statusBar">statusBar</a>()->message( QString( "Failed to save \'%1\'" ).
|
---|
| 81 | arg( m_filename ), 2000 );
|
---|
| 82 | return;
|
---|
| 83 | }
|
---|
| 84 | <a href="qtextstream.html">QTextStream</a> ts( &file );
|
---|
| 85 | for ( int i = 0; i < MAX_ELEMENTS; ++i )
|
---|
| 86 | if ( m_elements[i].isValid() )
|
---|
| 87 | ts << m_elements[i];
|
---|
| 88 |
|
---|
| 89 | file.<a href="qfile.html#close">close</a>();
|
---|
| 90 |
|
---|
| 91 | <a href="qwidget.html#setCaption">setCaption</a>( QString( "Chart -- %1" ).arg( m_filename ) );
|
---|
| 92 | <a href="qmainwindow.html#statusBar">statusBar</a>()->message( QString( "Saved \'%1\'" ).arg( m_filename ), 2000 );
|
---|
| 93 | m_changed = FALSE;
|
---|
| 94 | }
|
---|
| 95 | </pre>
|
---|
| 96 | <p> Saving data is equally easy. We open the file and create a text
|
---|
| 97 | stream. We then stream every valid element to the text stream. All the
|
---|
| 98 | detail is handled by the <tt>Element</tt> class.
|
---|
| 99 | <p> <p align="right">
|
---|
| 100 | <a href="tutorial2-06.html">« Canvas Control</a> |
|
---|
| 101 | <a href="tutorial2.html">Contents</a> |
|
---|
| 102 | <a href="tutorial2-08.html">Taking Data »</a>
|
---|
| 103 | </p>
|
---|
| 104 | <p>
|
---|
| 105 | <!-- eof -->
|
---|
| 106 | <p><address><hr><div align=center>
|
---|
| 107 | <table width=100% cellspacing=0 border=0><tr>
|
---|
| 108 | <td>Copyright © 2007
|
---|
| 109 | <a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
|
---|
| 110 | <td align=right><div align=right>Qt 3.3.8</div>
|
---|
| 111 | </table></div></address></body>
|
---|
| 112 | </html>
|
---|