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.5 6.2 Fixes: QTBUG-117900 Task-number: QTBUG-89145 Change-Id: I421e775130b03ce3eb2dd1dd05370e7391af087b Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io> (cherry picked from commit 0416e080cffe33de435631b5d47d3acbbcb7b880) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
parent
ef144f2e30
commit
7da3dda667
@ -1858,28 +1858,30 @@ QStandardItem *QStandardItem::takeChild(int row, int column)
|
|||||||
if (index != -1) {
|
if (index != -1) {
|
||||||
QModelIndex changedIdx;
|
QModelIndex changedIdx;
|
||||||
item = d->children.at(index);
|
item = d->children.at(index);
|
||||||
if (item && d->model) {
|
if (item) {
|
||||||
QStandardItemPrivate *const item_d = item->d_func();
|
QStandardItemPrivate *const item_d = item->d_func();
|
||||||
const int savedRows = item_d->rows;
|
if (d->model) {
|
||||||
const int savedCols = item_d->columns;
|
QStandardItemModelPrivate *const model_d = d->model->d_func();
|
||||||
const QVector<QStandardItem*> savedChildren = item_d->children;
|
const int savedRows = item_d->rows;
|
||||||
if (savedRows > 0) {
|
const int savedCols = item_d->columns;
|
||||||
d->model->d_func()->rowsAboutToBeRemoved(item, 0, savedRows - 1);
|
const QVector<QStandardItem*> savedChildren = item_d->children;
|
||||||
item_d->rows = 0;
|
if (savedRows > 0) {
|
||||||
item_d->children = QVector<QStandardItem*>(); //slightly faster than clear
|
model_d->rowsAboutToBeRemoved(item, 0, savedRows - 1);
|
||||||
d->model->d_func()->rowsRemoved(item, 0, savedRows);
|
item_d->rows = 0;
|
||||||
}
|
|
||||||
if (savedCols > 0) {
|
|
||||||
d->model->d_func()->columnsAboutToBeRemoved(item, 0, savedCols - 1);
|
|
||||||
item_d->columns = 0;
|
|
||||||
if (!item_d->children.isEmpty())
|
|
||||||
item_d->children = QVector<QStandardItem*>(); //slightly faster than clear
|
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);
|
item_d->setParentAndModel(nullptr, nullptr);
|
||||||
}
|
}
|
||||||
d->children.replace(index, nullptr);
|
d->children.replace(index, nullptr);
|
||||||
|
@ -120,6 +120,7 @@ private slots:
|
|||||||
void taskQTBUG_45114_setItemData();
|
void taskQTBUG_45114_setItemData();
|
||||||
void setItemPersistentIndex();
|
void setItemPersistentIndex();
|
||||||
void signalsOnTakeItem();
|
void signalsOnTakeItem();
|
||||||
|
void takeChild();
|
||||||
void createPersistentOnLayoutAboutToBeChanged();
|
void createPersistentOnLayoutAboutToBeChanged();
|
||||||
private:
|
private:
|
||||||
QStandardItemModel *m_model = nullptr;
|
QStandardItemModel *m_model = nullptr;
|
||||||
@ -1828,5 +1829,36 @@ void tst_QStandardItemModel::createPersistentOnLayoutAboutToBeChanged() // QTBUG
|
|||||||
QCOMPARE(layoutChangedSpy.size(), 1);
|
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)
|
QTEST_MAIN(tst_QStandardItemModel)
|
||||||
#include "tst_qstandarditemmodel.moc"
|
#include "tst_qstandarditemmodel.moc"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user