I want to sort values in specific column by using python code when I click th "Çiz" button right side. However it sorts according to first element if numbers are double or more. I realize that it accepts values as string. I look many questions in websites there is no answer for me. I do not want to write a script, there must be a solution for sorting because it seems so easy.
It is my script from Qt 5.10, here.
Here is my script - I wrote 0 for sorting acccording to first column:
QTableWidget.sortItems(0, QtCore.Qt.AscendingOrder)
But it accepts values as string not number value. Actually there is a script when user click header it sorts values but I do not want to had user clicked the header:
QTableWidget.setSortingEnabled(True)
Finally I found the solution thanks to Gary Hughes and here answer for my solution.
I make a quick example for trying:
mywidget = QTableWidget()
mywidget .insertColumn(0)
mywidget .insertColumn(1)
list1 = [25,1,7]
list2 = [3,15,1]
for num in range(len(list1)):
mywidget.insertRow(num)
item = QTableWidgetItem()
item.setData(Qt.EditRole, list1[num])
mywidget.setItem(num, 0, item)
mywidget.setItem(num, 1, QTableWidgetItem(str(list2[num])))
mywidget.sortItems(0, QtCore.Qt.AscendingOrder)
mywidget.show()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With