kien bui
kien bui

Reputation: 1820

How to mix QStandardItemModel

I made a TreeView using QStandardItemModel like below image A

enter image description here

and I also have a QStandardItemModel like below image B

Now, I want mix two Model together to made a new Model for a TreeView enter image description here

new TreeView has like below image C:

enter image description here

Example:

QStandardItemModel * fileTree = new QStandardItemModel();
QStandardItemModel * zipTree = new QStandardItemModel();
QStandardItemModel * fullTree = new QStandardItemModel();
......
......

fileTree is model for TreeView image A;

zipTree is model for TreeView image B;

How to mix them to make fullTree for TreeView image C.

update:

3 data have both a model, only data is different. I want to merge data for photo C form A + B. file a.zip -> folder a. and add file list of a (image B) to new folder (a)

P/S: Don't using QfileSystemModel

Upvotes: 0

Views: 536

Answers (1)

JustWe
JustWe

Reputation: 4484

QTreeView t;
QStandardItemModel a;
t.setModel(&a);
t.show();

I simplified the Image A, construct it as below:

QStandardItem* folder1 = new QStandardItem("folder1");
QStandardItem* zip = new QStandardItem("a.zip");
a.appendRow(folder1);
folder1->appendRow(zip);

Construct Image B:

QStandardItemModel b;
b.appendRow(new QStandardItem("filea"));
b.appendRow(new QStandardItem("fileb"));
b.appendRow(new QStandardItem("filec"));

"Mix" both as Image C:

zip->setText(zip->text().remove(".zip"));
for (int i = 0; i < b.rowCount(); i++) {
    zip->appendRow(b.takeItem(i));
}

Upvotes: 1

Related Questions