| 主页 | 所有的类 | 主要的类 | 注释的类 | 分组的类 | 函数 |
(从chartform_files.cpp展开。)
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(); // 确保我们拥有颜色
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;
}
载入数据组非常容易。我们打开文件并且创建一个文本流。当有数据要读的时候,我们把一个元素读入到element并且如果它是有效的,我们就把它插入到m_elements矢量。所有的细节都由Element类来处理。然后我们关闭文件并且更新标题和最近打开的文件列表。最后我们绘制图表并标明它没有被改变。
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;
}
保存数据一样地容易。我们打开文件并且创建一个文本流。然后我们把每一个有效元素写到文本留中。所有的细节都由Element类来处理。
| Copyright © 2002 Trolltech | Trademarks | 译者:Cavendish | Qt 3.0.5版
|