PDA

View Full Version : Save the structure as a model in a file



valgaba
14th July 2010, 20:35
I can Save the structure as a model in a file, treeview model.

QStandardItem *parentItem = CatalogoModel->invisibleRootItem();
for (int i = 0; i < 4; ++i) {
QStandardItem *item = new QStandardItem(QString("item %0").arg(i));
parentItem->appendRow(item);
parentItem = item;
}






QFile file("catalogo/catalogo.cat");

if(file.open(QIODevice::WriteOnly))
saveModel(&file, CatalogoModel); //code crash


//if(file.open(QIODevice::ReadOnly))
//loadModel(&file, CatalogoModel);




}

//************************************************** *****************
void FrmCatalogo::loadModel(QIODevice *device, QStandardItemModel *model){
QDataStream stream(device);
int rowCount, columnCount;
stream >> rowCount;
stream >> columnCount;

for(int row = 0; row < rowCount; row++)
for(int column = 0; column < columnCount; column++) {
QString item;
stream >> item;
QStandardItem * w_item = new QStandardItem(item);
model->setItem(row, column, w_item);

}
}

//************************************************** **************************
void FrmCatalogo::saveModel(QIODevice *device, QStandardItemModel *model){
QDataStream stream(device);
int rowCount = model->rowCount();
int columnCount = model->columnCount();
stream << rowCount;
stream << columnCount;

for(int row = 0; row < rowCount; row++)
for(int column = 0; column < columnCount; column++) {
stream << model->item(row, column)->text();


}
}

wysota
14th July 2010, 22:35
I can Save the structure as a model in a file, treeview model.
Good for you!

ChrisW67
15th July 2010, 05:02
I think the announcement and congratulations are premature judging by the comment on line 16.

Have you single-stepped the routine to see where it "crashes"?
Are you handling the tree structure correctly in your save routine?

valgaba
16th July 2010, 15:13
The problem is that it keeps all the engine functions, is there any way to keep all the engine functions?, Just save the parent object did any solution to this problem?

Thanks