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();
}
}
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();
}
}