Important: Please read the Qt Code of Conduct -

Sorting QTreeWidget after adding a row

  • Hello,

    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.

  • If you are inside a class that extends QTreeWidget you can call:
    To force update...
    But maybe, what you really need is to connect the signal from QTreeWidget:
    Or something like that, with a slot in your code (where you apply the sorting), so you are going to be sure that the method is going to be called after the proper update of that widget...

  • updateGeometry() doesn't work. It appears to be more to do with child widgets rather than items in the view.

    The itemChanged() signal however appears to work very nicely! It also means I can connect the signal once and then forget about manually calling sortItems() every time I need to sort.

    Thanks very much!

Log in to reply