-2

I just made a program with QTreeWidget.But I want to restore it to normal when I click a button,I mean it should be same as when it started. I learned how to remove the selected section that appears with a blue horizontal line.by

mytreeview.clearSelection()

check the imagethe blue tab line But if anyone expand the column width, check the 2nd imageenter image description here , "Type" column width has been increased from 1st picture.Then the question is how can i set it to normal means to my default value shown in picture-1 and imagine someone change the position of "Date Modified" to the place of "Size" column by dragging(position exchanged). Then How can i reset it to normal by pressing a button? This is some fundamental doubts about Qtreewidget so i have no code to write here. Any help will be appreciated Thank u

Hacker6914
  • 247
  • 1
  • 9
  • 1
    Plesase provide a [minimal, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – musicamante Oct 16 '20 at 16:14
  • actually this is some fundamental doubts about Qtreewidget so i have no code to write.i just need some function to solve my problem.I have edited my post.please check,tell me if u dont understand.@musicamante – Hacker6914 Oct 16 '20 at 17:44

1 Answers1

1

You can collect and store the section sizes using sectionSize() and restore it with resizeSection().

In order to restore the original logical indexes, just check the visualIndex() and if it doesn't match the logical index then use moveSection().

        # ...
        header = self.table.horizontalHeader()
        self.sectionSizes = [header.sectionSize(s) for s in range(header.count())]

    def resetSections(self):
        header = self.table.horizontalHeader()
        for section, size in enumerate(self.sectionSizes):
            if header.visualIndex(section) != section:
                header.moveSection(header.visualIndex(section), section)
            header.resizeSection(section, size)

I strongly suggest you to carefully read and study the documentation about QTableWidget (and its inherited classes: QTableView and QAbstractItemView) along with QHeaderView, as everything you were asking is clearly explained in those pages.

musicamante
  • 41,230
  • 6
  • 33
  • 58
  • Thank u! It helped me a lot. Actually the documentation is written for c++ thats why I can't understand it properly. – Hacker6914 Oct 16 '20 at 18:32
  • @Hacker6914 the official PyQt documentation is very limited and it's not in the interest of their maintainers to complete it, and that's because 95% of it would be just a *copy* of the official Qt documentation. While it might seem a bit overwhelming if you're not experienced with C++, in reality it's much easier than it seems: almost all functions behave exactly in the same way in python and have the same arguments, and the small exceptions can be still found looking for the PyQt related docs or even using `help(class.function)` in the python shell. It's just a matter of habit. – musicamante Oct 16 '20 at 18:45
  • Just remember two simple aspects: each function is listed with the following syntax: `returnType Class::function(positional arguments, [keyword arguments])`. Each argument is listed as `argumentType, argumentName`, and the "keyword arguments" are exactly the same as in python (so they can usually be omitted). `void` means that the function returns nothing, otherwise if you reimplement that function you *must* return that value type. Note that there are situations for which functions do *not* return data as they actually modify some of its arguments (which is usually not possible in python) -> – musicamante Oct 16 '20 at 18:51
  • -> In that case, PyQt behaves in a slightly different way than what the C++ documentation suggests. A typical case is `QValidator.validate()`: in C++ it just returns the state, and the `input` and `pos` values are modified "on the fly" if required (as C++ allows to send a *reference* to a variable). In PyQt that function actually returns all arguments: the state, the input and the position, so that it's possible to change those values and return them as required by Qt. – musicamante Oct 16 '20 at 18:53