0

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

目前它会像这样对项目进行排序

100, 10, 1, 2 

ETC..

它需要这样排序:

1,2,10,100

ETC

我目前正在使用

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

谢谢

山姆

4

2 回答 2

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)
于 2013-04-29T11:51:34.707 回答
1

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

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

于 2013-02-05T21:41:24.383 回答