Use RAII object to skip pending sorts in QTreeWidget

Cleanup: This patch does not change functionality.
In several places, sorting inside of the QTreeModel is disabled
and afterwards restored. There is a RAII class for this, which is
used in some places, but not in others. This patch introduces
more consistent usage of this RAII object.

Change-Id: I9802998ad31d0f9d4417824f72ff0abbb0c38a17
Reviewed-by: Ievgenii Meshcheriakov <ievgenii.meshcheriakov@qt.io>
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
bb10
Andreas Buhr 2021-07-07 18:01:36 +02:00
parent ff6156204d
commit c0b7d834b0
2 changed files with 9 additions and 19 deletions

View File

@ -112,8 +112,7 @@ public:
*/
QTreeModel::QTreeModel(int columns, QTreeWidget *parent)
: QAbstractItemModel(parent), rootItem(new QTreeWidgetItem),
headerItem(new QTreeWidgetItem), skipPendingSort(false)
: QAbstractItemModel(parent), rootItem(new QTreeWidgetItem), headerItem(new QTreeWidgetItem)
{
rootItem->view = parent;
rootItem->itemFlags = Qt::ItemIsDropEnabled;
@ -127,8 +126,7 @@ QTreeModel::QTreeModel(int columns, QTreeWidget *parent)
*/
QTreeModel::QTreeModel(QTreeModelPrivate &dd, QTreeWidget *parent)
: QAbstractItemModel(dd, parent), rootItem(new QTreeWidgetItem),
headerItem(new QTreeWidgetItem), skipPendingSort(false)
: QAbstractItemModel(dd, parent), rootItem(new QTreeWidgetItem), headerItem(new QTreeWidgetItem)
{
rootItem->view = parent;
rootItem->itemFlags = Qt::ItemIsDropEnabled;
@ -1563,11 +1561,8 @@ QTreeWidgetItem::QTreeWidgetItem(QTreeWidgetItem *parent, QTreeWidgetItem *after
QTreeWidgetItem::~QTreeWidgetItem()
{
QTreeModel *model = treeModel();
bool wasSkipSort = false;
if (model) {
wasSkipSort = model->skipPendingSort;
model->skipPendingSort = true;
}
QTreeModel::SkipSorting skipSorting(model);
if (par) {
int i = par->children.indexOf(this);
if (i >= 0) {
@ -1606,9 +1601,6 @@ QTreeWidgetItem::~QTreeWidgetItem()
children.clear();
delete d;
if (model) {
model->skipPendingSort = wasSkipSort;
}
}
/*!
@ -2008,8 +2000,7 @@ void QTreeWidgetItem::insertChild(int index, QTreeWidgetItem *child)
return;
if (QTreeModel *model = treeModel()) {
const bool wasSkipSort = model->skipPendingSort;
model->skipPendingSort = true;
QTreeModel::SkipSorting skipSorting(model);
if (model->rootItem == this)
child->par = nullptr;
else
@ -2033,7 +2024,6 @@ void QTreeWidgetItem::insertChild(int index, QTreeWidgetItem *child)
children.insert(index, child);
d->updateHiddenStatus(child, true);
model->endInsertItems();
model->skipPendingSort = wasSkipSort;
} else {
child->par = this;
children.insert(index, child);

View File

@ -155,7 +155,7 @@ private:
QList<QTreeWidgetItemIterator*> iterators;
mutable QBasicTimer sortPendingTimer;
mutable bool skipPendingSort; //while doing internal operation we don't care about sorting
mutable bool skipPendingSort = false; // no sorting during internal operations
bool inline executePendingSort() const;
bool isChanging() const;
@ -167,9 +167,9 @@ public:
{
const QTreeModel * const model;
const bool previous;
SkipSorting(const QTreeModel *m) : model(m), previous(model->skipPendingSort)
{ model->skipPendingSort = true; }
~SkipSorting() { model->skipPendingSort = previous; }
SkipSorting(const QTreeModel *m) : model(m), previous(model ? model->skipPendingSort : false)
{ if (model) model->skipPendingSort = true; }
~SkipSorting() { if (model) model->skipPendingSort = previous; }
};
friend struct SkipSorting;
};