0

i am trying to create a table using python and pyqt4.

at the moment it will sort the items like this

100, 10, 1, 2 

etc..

it needs to be sorted as such:

1,2,10,100

etc

i am currently using

self.table.setSortingEnabled(True)

but i think this is what is causing it to sort like that? could someone please help me to sort it correctly

full code for the window is here:

def BuildCustomerDetails(self):

    # Create Table
    self.mainLayout = QtGui.QGridLayout()

    # Construct table items

    db = sqlite3.connect("Database")
    cursor = db.cursor()

        #ID
    cursor.execute("""SELECT ID FROM Customer;""")
    items = cursor.fetchall()
    ID = []
    for row in items:
        item = row[0]
        ID.append(item)



        #First name
    cursor.execute("""SELECT FirstName FROM Customer;""")
    items = cursor.fetchall()
    firstName = []
    for row in items:
        item = row[0]
        firstName.append(item)
    height = len(items)

        #Surname
    cursor.execute("""SELECT Surname FROM Customer;""")
    items = cursor.fetchall()
    Surname = []
    for row in items:
        item = row[0]
        Surname.append(item)

        #First Line Address
    cursor.execute("""SELECT firstLineAddress FROM Customer;""")
    items = cursor.fetchall()
    firstLineAddress = []
    for row in items:
        item = row[0]
        firstLineAddress.append(item)

        #Second Line Address
    cursor.execute("""SELECT SecondLineAddress FROM Customer;""")
    items = cursor.fetchall()
    SecondLineAddress = []
    for row in items:
        item = row[0]
        SecondLineAddress.append(item)

        #Town
    cursor.execute("""SELECT Town FROM Customer;""")
    items = cursor.fetchall()
    Town = []
    for row in items:
        item = row[0]
        Town.append(item)

        #Postcode
    cursor.execute("""SELECT Postcode FROM Customer;""")
    items = cursor.fetchall()
    Postcode = []
    for row in items:
        item = row[0]
        Postcode.append(item)


    # TABLE
    self.table = QtGui.QTableWidget(height,7,self)

    self.horizontalHeaderItem1 = QtGui.QTableWidgetItem("ID")
    self.horizontalHeaderItem2 = QtGui.QTableWidgetItem("First Name")
    self.horizontalHeaderItem3 = QtGui.QTableWidgetItem("Surname")
    self.horizontalHeaderItem4 = QtGui.QTableWidgetItem("Address Line 1")
    self.horizontalHeaderItem5 = QtGui.QTableWidgetItem("Address Line 2")
    self.horizontalHeaderItem6 = QtGui.QTableWidgetItem("Town")
    self.horizontalHeaderItem7 = QtGui.QTableWidgetItem("Post Code")


    self.table.setHorizontalHeaderItem(0,self.horizontalHeaderItem1)
    self.table.setHorizontalHeaderItem(1,self.horizontalHeaderItem2)
    self.table.setHorizontalHeaderItem(2,self.horizontalHeaderItem3)
    self.table.setHorizontalHeaderItem(3,self.horizontalHeaderItem4)
    self.table.setHorizontalHeaderItem(4,self.horizontalHeaderItem5)
    self.table.setHorizontalHeaderItem(5,self.horizontalHeaderItem6)
    self.table.setHorizontalHeaderItem(6,self.horizontalHeaderItem7)

    self.table.setWindowFlags(Qt.Dialog)
    self.table.setSortingEnabled(True)

    ## ADD DATABASE ITEMS TO TABLE

        #ID
    for i in range(1, height):
        item = QtGui.QTableWidgetItem(ID[i])
        self.table.setItem(i,0,item)

        #product code
    for i in range(1, height):
        item = QtGui.QTableWidgetItem(firstName[i])
        self.table.setItem(i,1,item)

        #Surname
    for i in range(1, height):
        item = QtGui.QTableWidgetItem(Surname[i])
        self.table.setItem(i,2,item)

        #firstLineAddress
    for i in range(1, height):
        item = QtGui.QTableWidgetItem(firstLineAddress[i])
        self.table.setItem(i,3,item)

        #SecondLineAddress
    for i in range(1, height):
        item = QtGui.QTableWidgetItem(SecondLineAddress[i])
        self.table.setItem(i,4,item)

        #Town
    for i in range(1, height):
        item = QtGui.QTableWidgetItem(Town[i])
        self.table.setItem(i,5,item)

        #Postcode
    for i in range(1, height):
        item = QtGui.QTableWidgetItem(Postcode[i])
        self.table.setItem(i,6,item)


    # Create Widgets
    self.AddButton = QtGui.QPushButton("Add Customer",self)
    self.RemoveButton = QtGui.QPushButton("Remove Customer",self)
    self.MoreButton = QtGui.QPushButton("More Details",self)
    self.BackButton = QtGui.QPushButton("Back",self)

    # Create Layouts
    self.VLayout = QtGui.QVBoxLayout()
    self.HLayout = QtGui.QHBoxLayout()

    # Assemble

    self.VLayout.addWidget(self.AddButton)
    self.VLayout.addWidget(self.RemoveButton)
    self.VLayout.addWidget(self.MoreButton)
    self.VLayout.addWidget(self.BackButton)

    self.HLayout.addWidget(self.table)

    self.mainLayout.addLayout(TopBar(self),0,0,1,5)
    self.mainLayout.addLayout(self.VLayout,2,4)
    self.mainLayout.addLayout(self.HLayout,1,0,4,4)

    self.table.itemSelectionChanged.connect(self.getCell)


    self.MoreButton.clicked.connect(self.MoreCustomerDetailsLaunch)
    self.BackButton.clicked.connect(self.Back)
    self.AddButton.clicked.connect(self.AddCustomerLaunch)


    Menu(self)

    return self.mainLayout

thanks

sam

Sam McKay
  • 73
  • 1
  • 1
  • 9

2 Answers2

1

The QTableWidget class does not provide an API to customise the sorting process. If you want to you could try to reimplement QTableWidgetItem's __lt__ operator, at least this seems to work for C++ as seen in this answer.

An other way of achieving this could to sort the items when you add them to the table from the python side and avoid to set the sorting-enabled in the view. When you obtain the data from the database you should sort the columns by the column you want and only then create the QTableWidgetItems for them. Obviously this becomes quite complicated if you want to update the list dynamically, or if you want to change the order from ascending to descending.

Community
  • 1
  • 1
Bakuriu
  • 98,325
  • 22
  • 197
  • 231
1

reimplement QTableWidgetItem like this:

class MyTableWidgetItem(QtGui.QTableWidgetItem):
    def __init__(self, number):
        QtGui.QTableWidgetItem.__init__(self, number, QtGui.QTableWidgetItem.UserType)
        self.__number = number

    def __lt__(self, other):
        return self.__number < other.__number

so, where you need sorting by number use MyTableWidgetItem instead QTableWidgetItem, like this:

for i in range(1, height):
        item = MyTableWidgetItem(numbers[i])
        self.table.setItem(i,0,item)
Aleksandar
  • 3,541
  • 4
  • 34
  • 57