Start at the root of the tree, and recursively work your way down, calling each QTreeWidgetItem's isExpanded() method. If you make a bitstring, you can turn on the bit for each item that is expanded and turn it off for each one that is collapsed. Store this in QSettings. When you retrieve it, work you way back through it again, calling setExpanded() with true for the 1 bits, false for the zero bits.

But this only works if your tree never changes between the time you save it and the time it is loaded again. I don't know what your tree represents, but if the tree contents or structure can be changed from outside your program (like a file system, for example), then saving and restoring the state is meaningless.