This post has been edited 1 times, last edit by "bfarago" (Apr 30th 2010, 2:03pm)
|
|
Source code |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
void MainWindow::saveAsCSV()
{
QFile f( "table.csv" ); // #include <QtCore/QFile>
if( f.open( QIODevice::WriteOnly ) ){
QTextStream ts( &f ); // #include <QtCore/QTextStream>
QStringList strList;
for( int r = 0; r < tableWidget->rowCount(); ++r ){
strList.clear();
for( int c = 0; c < tableWidget->columnCount(); ++c ){
strList << "\""+tableWidget->item( r, c )->text()+"\"";
}
ts << strList.join( "," )+"\n";
}
f.close();
}
}
|
|
|
Source code |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
void MainWindow::load()
{
QFile f( "table.csv" ); // #include <QtCore/QFile>
if( f.open( QIODevice::ReadOnly ) ){
QTextStream ts( &f ); // #include <QtCore/QTextStream>
QList<QStringList> list;
int row = 0, col = 0;
// read entire file and parse lines into list of stringlist's
while( !ts.atEnd() ){
list << ts.readLine().split( "," );
}
f.close(); // done with file
// prep table
tableWidget->setRowCount( list.count() ); // number of stringlists give row count
tableWidget->setColumnCount( list[0].count() ); // count of entries from intial stringlist for column count
tableWidget->setUpdatesEnabled( false ); // for faster processing of large lists
foreach( QStringList l, list ){
foreach( QString str, l ){
// remove quotes if str quoted
if( str.endsWith( '"' ) ) str.chop(1);
if( str.startsWith( '"' ) ) str.remove(0,1);
tableWidget->setItem( row, col++, new QTableWidgetItem( str ));
}
row++; col=0;
}
tableWidget->setUpdatesEnabled( true ); // done with load
}
}
|
|
|
Source code |
1 2 3 4 5 6 |
for( int r = 0; r < tableWidget->rowCount(); ++r ){
strList.clear();
strList << "\""+tableWidget->item( r, 0 )->text()+"\"";
strList << "\""+tableWidget->item( r, 1 )->text()+"\"";
strList << "\""+tableWidget->item( r, 3 )->text()+"\""; //let's suppose the image is in the third column (that's why (...)->item(r,2)... is missing, and the fourth column (this one) is the hidden one storing "img".
}
|
This post has been edited 1 times, last edit by "bfarago" (Apr 30th 2010, 10:10pm)
Thank you so much, you've helped me a lot!
I am going further.
Apart from strings the table can contain images(.png) and time.
Storing and loading time is okay, as it is converted to QString when being put into the table. But what about if a cell contains an image?
Can i modify your write algorithm, or do sg else (maybe creating a temporary table)?
I would most likely to handle the image this way: if a cell contains an image, the value in the csv file would be .e.g "img". If it doesn't, value would be null.
Idk if it is possible to put a condition into the 11-13 lines of your saveAsCSV() function.
It can be a good idea, to create a hidden column in the table where I store "img" if there's an image in that row..then the saving procedure would look like this:
What do you suggest?
Quoted
I'm not sure why you are hidding columns with images, but that is your design and if it works for you then good.
What i did is this:
This post has been edited 1 times, last edit by "bfarago" (May 2nd 2010, 11:00am)
|
|
Source code |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
void todolist::saveClicked()
{
QFile f( "table.csv" );
if( f.open( QIODevice::WriteOnly ) )
{
QTextStream ts( &f );
QStringList strList;
for (int i=0; i<ui->tableWidget->rowCount(); i++)
{
strList.clear();
strList << "\""+ui->tableWidget->item(i,0)->text()+"\"";
strList << "\""+ui->tableWidget->item(i,1)->text()+"\"";
strList << "\""+ui->tableWidget->item(i,2)->text()+"\"";
strList << "\""+ui->tableWidget->item(i,3)->text()+"\"";
strList << "\""+ui->tableWidget->item(i,4)->text()+"\"";
strList << "\""+ui->tableWidget->item(i,5)->text()+"\"";
strList << "\""+ui->tableWidget->item(i,6)->text()+"\"";
strList << "\""+ui->tableWidget->item(i,7)->text()+"\"";
ts << strList.join(",") + "\n";
}
f.close();
}
}
|
|
|
Source code |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
void todolist::saveClicked()
{
QFile f( "table.csv" );
if( f.open( QIODevice::ReadWrite ) )
{
QTextStream ts( &f );
QStringList tmpList;
QStringList strList;
while( !ts.atEnd() ){
tmpList << ts.readLine();
}
QString tmpStr;
for (int i=0; i<ui->tableWidget->rowCount(); i++)
{
strList.clear();
strList << "\""+ui->tableWidget->item(i,0)->text()+"\"";
strList << "\""+ui->tableWidget->item(i,1)->text()+"\"";
strList << "\""+ui->tableWidget->item(i,2)->text()+"\"";
strList << "\""+ui->tableWidget->item(i,3)->text()+"\"";
strList << "\""+ui->tableWidget->item(i,4)->text()+"\"";
strList << "\""+ui->tableWidget->item(i,5)->text()+"\"";
strList << "\""+ui->tableWidget->item(i,6)->text()+"\"";
strList << "\""+ui->tableWidget->item(i,7)->text()+"\"";
tmpStr << strList.join(",");
if( !tmpList.contains( tmpStr ) ){
ts << tmpStr << "\n";
}
}
f.close();
}
}
|