Is there some way to Copy a QStandardItemModel to another QStandardItemModel? Or must I iterate over each tem and append it to the other Model?
: QAbstractItemModel::headerData(section, orientation, role); 2886 2887 2888 2889 eimp 2890 2891 QStandardItemModel supports both copy and move. 2892
QStandardItemModel.insertRow (self, int row, list-of-QStandardItem items) The itemsargument has it's ownership transferred to Qt. Inserts a row at rowcontaining items. If necessary, the column count is increased to the size of items. This function was introduced in Qt 4.2. See alsotakeRow(), appendRow(), and insertColumn().
QStandardItemQStandardItemModel.itemPrototype (self) Returns the item prototype used by the model. The model uses the item prototype as an item factory when it needs to construct new items on demand (for instance, when a view or item delegate calls setData()). This function was introduced in Qt 4.2. See alsosetItemPrototype().
An item can be owned by one model only. That is why you need to create a copy of each item and place it to another model. You can do it using method QStandardItem::clone. void copy (QStandardItemModel* from, QStandardItemModel* to) { to->clear (); for (int i = 0 ; i < from->rowCount () ; i++) { to->appendRow (from->item (i)->clone ()); } }
An item can be owned by one model only. That is why you need to create a copy of each item and place it to another model. You can do it using method QStandardItem::clone
.
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 linked views.
You could do copy of an existing item with next steps:
Or simply use QStandardItem::clone()
method. And reimplement it, if necessary.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With