DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 
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