Important: Please read the Qt Code of Conduct -

Slow adding of QTreeWidgetItems inside a tab, if another tab opened

  • Hi.
    In my application, I have a QTabWidget with 2 tabs. One contains a QTreeWidget and I do nothing in it. Second contains another QTreeWidget and a QTableWidget, and they are filled with data when reading a file. (10s of thousands of items in tree widget, thousands in table widget.)

    The problem is, when I have 1st tab opened and read the file, the loading takes roughly 20 seconds, and if I open 2nd tab before reading file - it takes 1 second or less. I used QElapsedTimer for measuring, and saw that in 1st case addChild() & addTopLevelItem() in tree widget start to work slower over time, and same goes for setVerticalHeaderItem() in table widget. No such problem when the 2nd tab is opened before reading.

    What could be the reason for this?
    I tried this, but nothing changed:

    • setUpdatesEnabled(false) for tree widget (in tab 2)
    • setVisible(false) for tree widget (in tab 2)
    • blockSignals() for tree widget (in tab 2) //edit: close, but not enough, see my next post
    • setUpdatesEnabled(false) for tab widget
    • setUpdatesEnabled(false) for the window

    Also, Qt 5.13.1, Windows 7 x64 SP1.

  • Lifetime Qt Champion

    As long as no code we can't say much but one thing: The QTreeWidget + QTreeWidgetItem is just a convenience class. If you've a lot of data create your own model.

  • Here is what helped. Did this before data loading:


    The person who posted this solution (here) had very similar problem with items in QTableWidget. Must have something to do with the way these items are created.

    Also in my case, could have been related to "visible" property of a widget, because problem also occured when my widgets were explicitly hidden.

Log in to reply