I am building a database for my work. I want to be able to sort the elements in a manner similar to how you can sort elements in excel by clicking the header of a column. I am using the pandas library so I plan on sorting with that, but my main question is is there a way to run code when a header is clicked in a QTableWidget? Or is this not possible?
Asked
Active
Viewed 4,470 times
1 Answers
11
You can do that by using the property of the QtableView :
table = QtWidgets.QTableWidget()
table.setSortingEnabled(True)

AlexLaur
- 335
- 4
- 13
-
1Thanks! However, I tried that option and when I click the headers, it doesnt sort the table at all. Is there another way I have to do it? – Chris Ray Jan 19 '20 at 16:22
-
Ok, normally it is working. Have you define a custom header for your table ? Have you got a piece of code ? – AlexLaur Jan 19 '20 at 17:16
-
@AlexLaur,Nothing special in my table and it doesn't work either (in C++ for me) – Jacques Dec 06 '20 at 13:19
-
@Jacques I just tested it again in order to sort a table by clicking on the header and it worked. I tested on python 2.7 with an old version of Pyside. But in the Qt doc 5.15, this property is alive: https://doc.qt.io/qt-5/qtableview.html#sortingEnabled-prop. I am wondering if i doesn't understand the original question now ^^ – AlexLaur Dec 07 '20 at 16:54