用数字排序QTableWidgetItem

时间:2013-02-05 18:51:18

标签: python pyqt qtablewidget

我正在尝试使用python和pyqt4创建一个表。

此刻它会对这样的项目进行排序

100, 10, 1, 2 

等。

需要对其进行排序:

1,2,10,100

我目前正在使用

self.table.setSortingEnabled(True)

但我认为这是导致它那样排序的原因?有人可以帮我正确排序

窗口的完整代码在这里:

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

谢谢

SAM

2 个答案:

答案 0 :(得分:1)

QTableWidget类不提供用于自定义排序过程的API。如果您愿意,可以尝试重新实现QTableWidgetItem的{​​{1}}运算符,至少这似乎适用于C ++,如this回答所示。

实现此目的的另一种方法是在从python端将它们添加到表中时对项进行排序,并避免在视图中设置启用排序。从数据库获取数据时,应按所需列对列进行排序,然后为它们创建__lt__。 显然,如果要动态更新列表,或者想要将顺序从升序更改为降序,这将变得非常复杂。

答案 1 :(得分:1)

重新实现QTableWidgetItem,如下所示:

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

所以,你需要按编号排序使用MyTableWidgetItem而不是QTableWidgetItem,如下所示:

for i in range(1, height):
        item = MyTableWidgetItem(numbers[i])
        self.table.setItem(i,0,item)