0

I have two QTreeView's.

First (QTreeView1) displayed folders, second (QTreeView2) - displayed subfolders, that are loaded on folder click in first QTreeView1. On click by folder in QTreeView1, I create QStandardItemModel with subfolders and set this model to QTreeView2. Also all items in both of QTreeView`s is checkable and I want to save all checked items state. How can I organize models storage for each loaded folders. Is it should be something like this:

// store folder model on subfolders check state changed
QMap<QStandardItemModel*, QString> modelStorage;
modelStorage.push_back(folderModel, folderPath);

and restore folder on folder click with:

QStandardItemModel* findFolderModel(QString folderPath)
{
    QStandardItemModel* model;
    foreach(auto path, modelStorage)
    {
        if (path == folderPath)
        {
            model = modelStorage.find(folderPath);
        }
        else model = nullptr;
    }

   return model;
}

and show model then ... Is it correct way to store all folder models? or it must be dynamically loaded? But in that case I need to store all model data by myself (for example, checked items state ...). Also model`s data can be changed for a while and I cant show "correct" data if I restore model from "snapshots".

UPD also I have question about implementation of my suggestion: I store/restore models on click by folders in QTreeView1 and it seems to be worked ... but restored models doesn't contains/displays QStandardItems. It happens because treeItem allocated with new operator in local scope? How can I save all QStandardItems in each model in that case?

model = new QStandardItemModel;
QStandardItem* treeItem = new QStandardItem("item");
model->appendRow(treeItem);
//..
modelStorage.insert(folderItem, model);
ui.treeView->setModel(model);
// after restore model pointer is valid, but hadn't contains any items.
Meteo ir3
  • 449
  • 8
  • 21

1 Answers1

1

I think, you can try to use two QFileSystemModels for your both QTreeViews and don't create QStandardItemModel every time you click item in first QTreeView. As you can see in documentation, QFileSystemModel have setRootPath method. You can use this method for second model every time you click on folder in first QTreeView.

To make your items checkable, browse this helpful articles:

http://www.qtcentre.org/threads/27253-QFileSystemModel-with-checkboxes

QFilesystemmodel with Checkboxes

http://doc.qt.io/qt-5/qidentityproxymodel.html

Community
  • 1
  • 1
Kirill Chernikov
  • 1,387
  • 8
  • 21
  • Thx for advice, but its my mistake that I have not explain that folders and subfolders is not file system structure ... its folders from specific database and I need to create QStandardItemModel and fill it manually. Any decision for this case? – Meteo ir3 Jul 15 '16 at 06:03
  • 1
    In this case, i think, you can use only one QStandardItemModel. For the first QTreeView show all your folders of this model. For the second QTreeView use reimplementation of QSortFilterProxyModel (under QStandardItemModel) to filter source QStandardItemModel by specific folder, which you determine from the first QTreeView. – Kirill Chernikov Jul 15 '16 at 07:01
  • amm) thx again, but structure in QTreeView1 doesn't contains subfolders from QTreeView2. Its different because folders from QTreeView1 lazy loads when it is needed. And this is the reason why I cant store all data in one model (time to load whole database). Its motivate me divide models on folder Model with "keys" and subfolder models with items for keys in QTreeView1 model) Have you any opinion about that? – Meteo ir3 Jul 15 '16 at 07:36
  • In this case, your approach is justified.But, i think, it will be better to create this models in dynamic (from database, as i understand). And every time, when you change model for second QTreeView (by clicking at the first QTreeView), save data changes in your database. No "snapshots", no synchronization problem between "snapshots" and database data. – Kirill Chernikov Jul 15 '16 at 08:07
  • it will be nice, but I have read only access to database - its reason for using "snapshots"... Do you know decision for problem described in 1st post UPD? How can I store in modelStorage all related QStandardItems for each model? (now it destroyed on out of scope... I need deep copy of model? How can I realize that?) – Meteo ir3 Jul 15 '16 at 09:06
  • I don't see problem in your code. Everything looks all right. Maybe, you do some another manipulating with models, which is not in your question? – Kirill Chernikov Jul 15 '16 at 09:29