PyQt5基础学习-QTableWidget().sortItems(对表格某一列进行排序) 1.Qt.DescendingOrder(降序) 2.Qt.AscendingOrder(升序)
使用QTableWidget().sortItems()对某一列进行升序或者降序, 使用按钮进行排序,原来是升序点击完以后就是降序
""" 按列排序 1. 按哪一列排序 2. 排序类型: 升序或降序 sortItems(columnIndex, orderType) """ import sys from PyQt5.QtWidgets import * from PyQt5 import QtCore from PyQt5.QtGui import QColor, QBrush from PyQt5 import QtWidgets, Qt from PyQt5.QtGui import QFont from PyQt5.Qt import * class ColumnSort(QWidget): def __init__(self): super(ColumnSort, self).__init__() self.initUI() def initUI(self): self.setWindowTitle("按列排序") self.resize(430, 230) layout = QVBoxLayout() self.tableWidget = QTableWidget() self.tableWidget.setRowCount(4) self.tableWidget.setColumnCount(3) layout.addWidget(self.tableWidget) self.tableWidget.setHorizontalHeaderLabels(['姓名', '性别', '体重(kg)']) newItem = QTableWidgetItem("张三") self.tableWidget.setItem(0, 0, newItem) newItem = QTableWidgetItem("男") self.tableWidget.setItem(0, 1, newItem) newItem = QTableWidgetItem("165") self.tableWidget.setItem(0, 2, newItem) newItem = QTableWidgetItem("李四") self.tableWidget.setItem(1, 0, newItem) newItem = QTableWidgetItem("女") self.tableWidget.setItem(1, 1, newItem) newItem = QTableWidgetItem("160") self.tableWidget.setItem(1, 2, newItem) newItem = QTableWidgetItem("王五") self.tableWidget.setItem(2, 0, newItem) newItem = QTableWidgetItem("女") self.tableWidget.setItem(2, 1, newItem) newItem = QTableWidgetItem("170") self.tableWidget.setItem(2, 2, newItem) self.button = QPushButton('排序') self.button.clicked.connect(self.order) layout.addWidget(self.button) self.orderType = Qt.DescendingOrder self.setLayout(layout) def order(self): if self.orderType == Qt.DescendingOrder: self.orderType = Qt.AscendingOrder else: self.orderType = Qt.DescendingOrder #对第三列进行升序或者降序操作 self.tableWidget.sortItems(2, self.orderType) if __name__ == "__main__": app = QtWidgets.QApplication(sys.argv) main = ColumnSort() main.show() sys.exit(app.exec_())
每天更新pyQt5内容