QStandardItem: Fix reset parent in takeItem()

After the change for QTBUG-89145 the parent was not set to 0 when the
item was not attached to a model.

Pick-to: 6.6 6.5 6.2
Fixes: QTBUG-117900
Task-number: QTBUG-89145
Change-Id: I421e775130b03ce3eb2dd1dd05370e7391af087b
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
This commit is contained in:
Christian Ehrlicher 2023-11-11 10:26:08 +01:00 committed by Volker Hilsheimer
parent acfd2a4bb0
commit 0416e080cf
2 changed files with 53 additions and 19 deletions

View File

@ -1858,28 +1858,30 @@ QStandardItem *QStandardItem::takeChild(int row, int column)
if (index != -1) {
QModelIndex changedIdx;
item = d->children.at(index);
if (item && d->model) {
if (item) {
QStandardItemPrivate *const item_d = item->d_func();
const int savedRows = item_d->rows;
const int savedCols = item_d->columns;
const QVector<QStandardItem*> savedChildren = item_d->children;
if (savedRows > 0) {
d->model->d_func()->rowsAboutToBeRemoved(item, 0, savedRows - 1);
item_d->rows = 0;
item_d->children = QVector<QStandardItem*>(); //slightly faster than clear
d->model->d_func()->rowsRemoved(item, 0, savedRows);
}
if (savedCols > 0) {
d->model->d_func()->columnsAboutToBeRemoved(item, 0, savedCols - 1);
item_d->columns = 0;
if (!item_d->children.isEmpty())
if (d->model) {
QStandardItemModelPrivate *const model_d = d->model->d_func();
const int savedRows = item_d->rows;
const int savedCols = item_d->columns;
const QVector<QStandardItem*> savedChildren = item_d->children;
if (savedRows > 0) {
model_d->rowsAboutToBeRemoved(item, 0, savedRows - 1);
item_d->rows = 0;
item_d->children = QVector<QStandardItem*>(); //slightly faster than clear
d->model->d_func()->columnsRemoved(item, 0, savedCols);
model_d->rowsRemoved(item, 0, savedRows);
}
if (savedCols > 0) {
model_d->columnsAboutToBeRemoved(item, 0, savedCols - 1);
item_d->columns = 0;
item_d->children = QVector<QStandardItem*>(); //slightly faster than clear
model_d->columnsRemoved(item, 0, savedCols);
}
item_d->rows = savedRows;
item_d->columns = savedCols;
item_d->children = savedChildren;
changedIdx = d->model->indexFromItem(item);
}
item_d->rows = savedRows;
item_d->columns = savedCols;
item_d->children = savedChildren;
changedIdx = d->model->indexFromItem(item);
item_d->setParentAndModel(nullptr, nullptr);
}
d->children.replace(index, nullptr);

View File

@ -120,6 +120,7 @@ private slots:
void taskQTBUG_45114_setItemData();
void setItemPersistentIndex();
void signalsOnTakeItem();
void takeChild();
void createPersistentOnLayoutAboutToBeChanged();
private:
QStandardItemModel *m_model = nullptr;
@ -1828,5 +1829,36 @@ void tst_QStandardItemModel::createPersistentOnLayoutAboutToBeChanged() // QTBUG
QCOMPARE(layoutChangedSpy.size(), 1);
}
void tst_QStandardItemModel::takeChild() // QTBUG-117900
{
{
// with model
QStandardItemModel model1;
QStandardItemModel model2;
QStandardItem base1("base1");
model1.setItem(0, 0, &base1);
QStandardItem base2("base2");
model2.setItem(0, 0, &base2);
auto item = new QStandardItem("item1");
item->appendRow({new QStandardItem("child")});
base1.appendRow({item});
base2.appendRow({base1.takeChild(0, 0)});
QCOMPARE(base1.child(0, 0), nullptr);
QCOMPARE(base2.child(0, 0), item);
}
{
// without model
QStandardItem base1("base1");
QStandardItem base2("base2");
auto item = new QStandardItem("item1");
item->appendRow({new QStandardItem("child")});
base1.appendRow({item});
base2.appendRow({base1.takeChild(0, 0)});
QCOMPARE(base1.child(0, 0), nullptr);
QCOMPARE(base2.child(0, 0), item);
}
}
QTEST_MAIN(tst_QStandardItemModel)
#include "tst_qstandarditemmodel.moc"