Meteo ir3
Meteo ir3

Reputation: 449

How can I organize QStandardItemModel`s in one QTreeView

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.

Upvotes: 0

Views: 420

Answers (1)

Kirill Chernikov
Kirill Chernikov

Reputation: 1420

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

Upvotes: 1

Related Questions