Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sort QTableWidget column values? [duplicate]

enter image description hereI 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)
like image 918
Mustafa Uçar Avatar asked May 08 '18 06:05

Mustafa Uçar


Video Answer


1 Answers

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()
like image 97
Mustafa Uçar Avatar answered Nov 11 '22 18:11

Mustafa Uçar