int row = rowCount(); // number of folders already in the model
insertRow(row); // add a new row
QModelIndex ind
= index
(row,
0);
// index of the newly created item insertColumn(0, ind); // add a column for future children of the item
setData(ind, foldername, Qt::DisplayRole);
setData(ind, imageFile, ImageRole); // ImageRole = Qt::UserRole
setData
(ind,
QPixmap(rootPath
+"/"+foldername
+"/"+imageFile
+".jpg"), ImageDataRole
);
// ImageDataRole = ImageRole+1; (this keeps the actual image) return ind;
}
int row = rowCount(parent); // get number of files
insertRow(row, parent); // insert new file
setData(ind, filename, Qt::DisplayRole); // insert filename
QString filePath
= rootPath
+"/"+data
(parent, Qt
::DisplayRole).
toString()+"/"+filename
+".tst";
// assemble file path setData(ind, filePath, PathRole); // PathRole = ImageDataRole+1
return ind;
}
QModelIndex MyModel::addFolder(const QString &foldername, const QString &imagefile){
int row = rowCount(); // number of folders already in the model
insertRow(row); // add a new row
QModelIndex ind = index(row, 0); // index of the newly created item
insertColumn(0, ind); // add a column for future children of the item
setData(ind, foldername, Qt::DisplayRole);
setData(ind, imageFile, ImageRole); // ImageRole = Qt::UserRole
setData(ind, QPixmap(rootPath+"/"+foldername+"/"+imageFile+".jpg"), ImageDataRole); // ImageDataRole = ImageRole+1; (this keeps the actual image)
return ind;
}
QModelIndex MyModel::addFile(const QModelIndex &parent, const QString &filename){
int row = rowCount(parent); // get number of files
insertRow(row, parent); // insert new file
QModelIndex ind = index(row, 0, parent); // get index
setData(ind, filename, Qt::DisplayRole); // insert filename
QString filePath = rootPath+"/"+data(parent, Qt::DisplayRole).toString()+"/"+filename+".tst"; // assemble file path
setData(ind, filePath, PathRole); // PathRole = ImageDataRole+1
return ind;
}
To copy to clipboard, switch view to plain text mode
Then you can use these like so:
MyModel model(rootPath); // rootPath holds the path to the "/docs" folder
QModelIndex fld
= model.
addFolder("doc_1",
"imageInDoc1");
QModelIndex fld2
= model.
addFolder("doc_2",
"imageInDoc2");
// etc.
MyModel model(rootPath); // rootPath holds the path to the "/docs" folder
QModelIndex fld = model.addFolder("doc_1", "imageInDoc1");
QModelIndex fil1 = model.addFile(fld, "file_1");
QModelIndex fil2 = model.addFile(fld, "file_2");
QModelIndex fld2 = model.addFolder("doc_2", "imageInDoc2");
// etc.
To copy to clipboard, switch view to plain text mode
Bookmarks