PDA

View Full Version : parse items of QTreeWidget into file/QSettings



Mystical Groovy
7th November 2009, 12:51
Good morning everyone

does anyone know how to parse the items of a QTreeWidget into a file?
Im trying to save the list of items in QTreeWIdget with QSettigns but currentItem()->text(0);
works only for one item

Iv tried to do : selectAll(); and then currentItem()->text(0) but it freezes my program..

Some samples Ive tried

Trying to parse the items on QSettings value


treeWidget->selectAll();
settings.setValue("repositoriesLastState",treeWidget->currentItem()->text(0));
the above on freezes my program, segmentation fault....

Trying to parse the items on a file for futher processing later.


QFile repos("/tmp/repositories");
repos.open(QFile::WriteOnly | QFile::Truncate);
QTextStream repoList(&repos);
repoList << treeWidget->currentItem()->text(0);

repos.close();
the above write to /tmp/repositories file only one item

Please help, thank you :)

Mystical Groovy
8th November 2009, 12:30
ive managed to do the following:



//where "repoList" is my treeWidget

QFile repositories("/tmp/repositories");
repositories.open(QFile::WriteOnly | QFile::Truncate);
QTextStream repositoryList(&repositories);
QTreeWidgetItem *parent;

int count =
parent ? parent->childCount() : repoList->topLevelItemCount();

for (int i = 0; i < count; i++)
{
QTreeWidgetItem *item =
parent ? parent->child(i) : repoList->topLevelItem(i);
// do something with the item
repositoryList << repoList->topLevelItem(i)->text(0);
}
repositories.close();


I found the above code in an old thread of qt-interest
The thing is, that even if the above code works nicely, saves my items in one line.

for example, my items in the QTreeWidget is like this:

test repo1
test repo2
test repo3
The above code saves it like this:

test repo 1test repo 2test repo 3

Ive tried to customize the code but no work...
any suggestions?

Lykurg
8th November 2009, 14:28
Well if you want linebreaks use:
repositoryList << "\n";

But you should consider using a XML or any other structured file (e.g. csv) to save your items...

Mystical Groovy
8th November 2009, 16:52
ok now i feel stupid...
thank you :)