PDA

View Full Version : getting data out of the simple tree model example



qt_gotcha
7th July 2009, 06:43
Fooling around with the treemodel, I hoped this would work. I want to wrte the contents of the treemodel in the simpletreemodel example to a text file. I thought the index function would get the correct data from the child items if the column counter > 0, so that the contents of the children are simply accessible with model.data(i,j). All I get is a list of contents of the parent items (so at index i,0) , I cannot acces the content of the children.


QFile fout("some.txt");
fout.open(QIODevice::ReadWrite);
for (int i = 0; i < model.rowCount(); i++)
{
QString S = "";
for (int j = 0; j < model.columnCount(); j++)
{
QVariant d = model.data(model.index(i,j),0);
S = S + d.toString()+";";
}
S = S + "\n";
QByteArray line(S.toAscii());
fout.write(line);
}
fout.close();

my reasoning is not correct obviously, but I don't get it. Thanks

qt_gotcha
7th July 2009, 07:13
ok, I am thinking about this as a table, every parent has a different nr children, and each child has several strings in my case (a data input structure for some software I am making). This works better :

QFile fout("hup.txt");
fout.open(QIODevice::ReadWrite);
for (int i = 0; i < model.rowCount(); i++)
{
QVariant d = model.data(model.index(i,0),0); // parent
QString S = "["+QString::number(i)+"-" + QString::number(0) + "] " + d.toString()+";\n";

QModelIndex indexParent = model.index(i, 0);
for (int j = 0; j < model.rowCount(indexParent); j++)
{
S = S + "["+QString::number(i)+"-" + QString::number(j) + "] ";

for (int k = 0; k < model.columnCount(indexParent); k++)
{
d = model.data(model.index(j, k, indexParent),0);
S = S + d.toString()+";";
}
S = S + "\n";
}
QByteArray line(S.toAscii());
fout.write(line);
}
fout.close();