View Full Version : Sorting QTreeWidget after adding a row

19th February 2011, 02:21

I’m programmatically sorting a QTreeWidget using sortItems(int column, Qt::SortOrder order). It works, of course. However, if I do it right after adding some items to the tree, the sort won’t take effect.

I’ve even tried calling sortItems from a one-shot QTimer. This works a little bit better, but still fails 50% of the time. Clearly something needs to be “updated” or validated in the QTreeWidget before the sort will work.

Is there a way to force the QTreeWidget to update so I can call sortItems immediately?

Many thanks.

19th February 2011, 07:45
how and where do you call the sort function and how for example do you add a new widget? The error is most probably in the order of calling these functions.

20th February 2011, 22:13
Hi there, I'm doing things like this:

QTreeWidgetItem *newItem = new QTreeWidgetItem("Foo");

newItem = new QTreeWidgetItem("Bar");

// etc. etc....

// after all items have been added, we then call the following within the same function:
theTree->sortItems(0, Qt::AscendingOrder);

Of course things are a little more complicated than that, with multiple columns etc. But this is how things are added, sorted, and in the order that it happens. I wouldn't think it could be any simpler than this!

Running Qt 4.6.2 if that makes any difference.