From bd0f3345a938b35ce6a12f6150373b0955b8dd12 Mon Sep 17 00:00:00 2001 From: Timothy Pearson Date: Sun, 10 Jul 2011 15:24:15 -0500 Subject: Add Qt3 development HEAD version --- doc/html/tutorial2-07.html | 112 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 doc/html/tutorial2-07.html (limited to 'doc/html/tutorial2-07.html') diff --git a/doc/html/tutorial2-07.html b/doc/html/tutorial2-07.html new file mode 100644 index 0000000..6298a38 --- /dev/null +++ b/doc/html/tutorial2-07.html @@ -0,0 +1,112 @@ + + + + + +File Handling + + + + + + + +
+ +Home + | +All Classes + | +Main Classes + | +Annotated + | +Grouped Classes + | +Functions +

File Handling

+ + +

+

(Extracts from chartform_files.cpp.) +

Reading Chart Data +

+

+ +

    void ChartForm::load( const QString& filename )
+    {
+        QFile file( filename );
+        if ( !file.open( IO_ReadOnly ) ) {
+            statusBar()->message( QString( "Failed to load \'%1\'" ).
+                                    arg( filename ), 2000 );
+            return;
+        }
+
+        init(); // Make sure we have colours
+        m_filename = filename;
+        QTextStream ts( &file );
+        Element element;
+        int errors = 0;
+        int i = 0;
+        while ( !ts.eof() ) {
+            ts >> element;
+            if ( element.isValid() )
+                m_elements[i++] = element;
+
        file.close();
+
+

        setCaption( QString( "Chart -- %1" ).arg( filename ) );
+        updateRecentFiles( filename );
+
+        drawElements();
+        m_changed = FALSE;
+    }
+
+

Loading a data set is very easy. We open the file and create a text +stream. While there's data to read we stream an element into element and if it is valid we insert it into the m_elements vector. +All the detail is handled by the Element class. Then we close +the file and update the caption and the recent files list. Finally we +draw the chart and mark it as unchanged. +

Writing Chart Data +

+

    void ChartForm::fileSave()
+    {
+
        QFile file( m_filename );
+        if ( !file.open( IO_WriteOnly ) ) {
+            statusBar()->message( QString( "Failed to save \'%1\'" ).
+                                    arg( m_filename ), 2000 );
+            return;
+        }
+        QTextStream ts( &file );
+        for ( int i = 0; i < MAX_ELEMENTS; ++i )
+            if ( m_elements[i].isValid() )
+                ts << m_elements[i];
+
+        file.close();
+
+        setCaption( QString( "Chart -- %1" ).arg( m_filename ) );
+        statusBar()->message( QString( "Saved \'%1\'" ).arg( m_filename ), 2000 );
+        m_changed = FALSE;
+    }
+
+

Saving data is equally easy. We open the file and create a text +stream. We then stream every valid element to the text stream. All the +detail is handled by the Element class. +

+« Canvas Control | +Contents | +Taking Data » +

+

+ +


+ +
Copyright © 2007 +TrolltechTrademarks +
Qt 3.3.8
+
+ -- cgit v1.2.3