6

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)
Mustafa Uçar
  • 442
  • 1
  • 6
  • 18
  • On this example, it automatically add values and 2 more columns. I have only two columns and I want to order them according to first one and other one should be affacted, as well. – Mustafa Uçar May 08 '18 at 07:23

1 Answers1

9

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()
Mustafa Uçar
  • 442
  • 1
  • 6
  • 18