PDA

View Full Version : saveFile



dragon
30th January 2006, 19:02
Hello anyone,

I have a QTreeWidget with a saveFile function.
I want to save 4 items in a text file with a tab between the items on a row.
This code save the items. 1 item on 1 row.
See code:


void MainWindow::saveFile(const QString &fileName)
{

QFile file( fileName );
if ( !file.open(QFile::WriteOnly))
return;

QTextStream out( &file );

int ind = 0;
while(QTreeWidgetItem *item = contactView->topLevelItem(ind)) {
for ( unsigned int i = 0; i < 4; i++ )
out << item->text(i) << "\n";

ind ++;
}
file.close();

}

Must i use QStringlist with join or can i change the code a litle bit.

axeljaeger
30th January 2006, 20:04
Try something like this:



void MainWindow::saveFile(const QString &fileName)
{

QFile file( fileName );
if ( !file.open(QFile::WriteOnly))
return;

QTextStream out( &file );

int ind = 0;
while(QTreeWidgetItem *item = contactView->topLevelItem(ind)) {
for ( unsigned int i = 0; i < 4; i++ )
out << item->text(i) << '\t';
out << '\n';

ind ++;
}
file.close();

}

dragon
30th January 2006, 21:09
Thanks axeljaeger for your answer.
It works fine now in the textfile with tabs between the item's.

Mariane
30th January 2006, 23:26
Please, Dragon, how did you manage to keep the indentation
in your code? I've tried both tabs and white spaces and my
code comes out flat on the left margin...?

Mariane

jacek
30th January 2006, 23:32
how did you manage to keep the indentation
in your code? I've tried both tabs and white spaces and my
code comes out flat on the left margin...?
That's what [ code ] tags are for, you can insert them using # button in the editor or just type them.