I've got a Qt related question (for c++) regarding the QTreeWidget and hope someone can help.
I am working with the QTreeWidget and have run into a performance issue that I can't seem to solve. I basically have made a simple extension of it that allows me to easily filter the tree (hide items depending on filters). Running through all top level items and hiding/showing them depending on the filter is very easy, however it slows down whenever the tree is allowed to be sorted (e.g. by calling setSortingEnabled(true) on the QTreeWidget). Does anyone know a way around this or a better way to do this?
The way I filter the the items is as follows:
setUpdatesEnabled(false);
for(int i = 0; i < topLevelItemCount(); ++i)
{
// Retrieve item
const auto pItem = topLevelItem( i );
bool hide = false;
for(filter : mFilters){
// Stop if a filter already hides the item
if(hide) break;
// Check filter
hide = filter.checkFilter(pItem);
}
pItem->setHidden(false);
}
setUpdatesEnabled(true);
This function itself is very fast, it can run in one or two seconds for a test with 1 million items. However once setSortingEnabled(true) is called to also allow sorting it slows down significantly. I've checked with debugging and it's not the sorting that slows things down, the function above will now take ages to complete. It will now take a few minute at least compared to the mere seconds it took previously (when sorting is not allowed).
To me it appears that if sorting is enabled even once then the call
const auto pItem = topLevelItem( i );
or
pItem->setHidden(hide)
slows down significantly.
Can anyone shed some light on this issue or suggest a faster/better way to apply filters and/or loop through all top level items in the QTreeWidget?
I've looked online for faster ways to loop through the top level items, but most suggest topLevelItem(int i). I've not seen any thread regarding slowdown after sorting is enabled.
I've tried a few things, namely
- Using QTreeWidgetIterators rather than topLevelItem(int i)
- Calling setUpdatesEnabled(false) and setUpdatesEnabled(true) before and after the filter check respectively
- Calling setSortingEnabled(false) and setSortingEnabled(true) before and after the filter check respectively
- Retrieving all items in a QList findItems when searching for a wildcard string and looping over the returned QList
- Looping over the QList<QTreeWidgetItem*> with pointers to items used when the tree was populated via addTopLevelItems(list)
None of these have had any impact on the performance unfortunately. If sorting has ever been enabled, then looping over items slows down significantly.