Copy QStandardItemModel to another

Is there a way to copy the QStandardItemModel to another QStandardItemModel? Or should I iterate over each topic and add it to another model?

+5
source share
2 answers

An item can only belong to one model. This is why you need to create a copy of each element and put it in another model. You can do this using the QStandardItem::clone method.

This is an example for single-column models:

 void copy(QStandardItemModel* from, QStandardItemModel* to) { to->clear(); for (int i = 0 ; i < from->rowCount() ; i++) { to->appendRow(from->item(i)->clone()); } } 

EDIT:
Use to->removeRows(0, to->rowCount ()); instead of to->clear(); if you want to keep header data and column sizes in related views.

+7
source

You can make a copy of an existing item with the following steps:

  • Get an existing item.
  • Create a new item.
  • Install the required data roles from an existing item to a new one.
  • Do the same with the flags.

Or just use the QStandardItem::clone() method. And redefine it if necessary.

+1
source

Source: https://habr.com/ru/post/1202733/


All Articles