void visualICDAR::open()
{QString fileName = QFileDialog::getOpenFileName( this, tr( "Open Image" ),
QDir::homePath(), tr("Images ( *.tif )") );
if( !fileName.isEmpty() )
{
QApplication::setOverrideCursor( QCursor( Qt::WaitCursor ) );
pixmapWidget->setPixmap( fileName );
QString dataFileName = fileName.replace( fileName.length() - 3, 3, "csv" );
QFile data( dataFileName );
if( !data.open(QIODevice::ReadOnly|QIODevice::Text) )
{
QMessageBox::warning( this, "NoCSV file founs!",
QString( "No %1 file was found!" ).arg( dataFileName ) );
}
QTextStream text( &data );
text.setCodec( "UTF-8" );
QString dataString = text.readAll();
( (QStandardItemModel*)fileWidget->model() )->clear();
fileWidget->AddRowsFromString( dataString );
fileWidget->setOutputFile( dataFileName );
data.close();
QApplication::restoreOverrideCursor();
}
}
class CFileWidget : public QTableView
{
Q_OBJECT
public:
CFileWidget(QWidget *parent);
~CFileWidget();
void setOutputFile( const QString fileName )
{ outputFile.setFileName(fileName);
outputFile.open(QIODevice::WriteOnly|QIODevice::Text); }
public slots:
void AddRowsFromString( const QString& text );
void DeleteSelectedRows();
void AddRow();
void SaveToFile();
signals:
void SelectRects( const QList<QRect>& rects, const QList<QRect>& parenRects );
protected slots:
void selectionChanged ( const QItemSelection & selected, const QItemSelection & deselected );
private:
int addonId;
QFile outputFile;
};
void CFileWidget::SaveToFile()
{
QTextStream textStream( &outputFile );
textStream.setCodec( "UTF-8" );
for( int row = 0; row < ( (QStandardItemModel*)model() )->rowCount(); ++row )
{
int column = 0;
while( ( (QStandardItemModel*)model() )->item( row, column ) )
{
textStream<<( (QStandardItemModel*)model() )->item( row, column )->data( Qt::DisplayRole ).toString();
textStream<<"\t";
column++;
}
textStream<<"\n";
}
}
我正在尝试从文件中读取一些数据,但是当我读取它时,文件变空了。为什么会这样?如果程序中出现一些错误,它不会保存任何数据,我们会丢失吗?那么如何打开文件,从中读取并将所有数据保存在其中?据我了解,如果我以只写模式再次打开它,我将重写所有数据而不是附加?
关于代码第一个功能是打开文件第二个代码是存储QFile的类,第三个是保存到文件功能