在 QTableWidget 表格中设置表格按列排序 的案例
1.按那一列排序
2.排序类型:升序降序
import sys
from PyQt5 import QtPrintSupport, QtGui
from PyQt5.QtCore import Qt, QMimeData, QDate, QDateTime, QTime, QStringListModel
from PyQt5.QtGui import QIcon, QPainter, QBrush, QPixmap, QStandardItemModel, QStandardItem, QColor, QFont
from PyQt5.QtPrintSupport import QPageSetupDialog, QPrinter, QPrintDialog
from PyQt5.QtWidgets import QApplication, QWidget, QComboBox, QFormLayout, QLabel, QLineEdit, QPushButton, QGridLayout, \
QCalendarWidget, QVBoxLayout, QDateTimeEdit, QAction, QMainWindow, QTextEdit, QStatusBar, QFileDialog, QDialog, \
QTableView, QMessageBox, QListView, QListWidget, QHBoxLayout, QTableWidget, QTableWidgetItem, QAbstractItemView
'''
在 QTableWidget 表格中设置表格按列排序 的案例
1.按那一列排序
2.排序类型:升序降序
sortItems(columnIndex,orderType)
'''
class ColumnSortDemo(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
# 设置定位和左上角坐标
self.setGeometry(300, 300, 460, 360)
# 设置窗口标题
self.setWindowTitle('QTableWidget扩展表格的按列排序 的演示')
# 设置窗口图标
# self.setWindowIcon(QIcon('../web.ico'))
layout = QVBoxLayout()
self.tablewidget = QTableWidget()
self.tablewidget.setRowCount(4)
self.tablewidget.setColumnCount(3)
layout.addWidget(self.tablewidget)
self.tablewidget.setHorizontalHeaderLabels(['姓名','性别','体重(kg)'])
newItem = QTableWidgetItem('玉皇大帝')
newItem.setFont(QFont('微软雅黑',14,QFont.Black))
self.tablewidget.setItem(0,0,newItem)
newItem = QTableWidgetItem('男')
newItem.setFont(QFont('宋体',14,QFont.Black))
self.tablewidget.setItem(0,1,newItem)
newItem = QTableWidgetItem('180')
newItem.setFont(QFont('宋体',28,QFont.Black))
newItem.setForeground(QBrush(QColor(0,120,255)))
self.tablewidget.setItem(0,2,newItem)
newItem = QTableWidgetItem('何仙姑')
newItem.setFont(QFont('微软雅黑',14,QFont.Black))
self.tablewidget.setItem(1,0,newItem)
newItem = QTableWidgetItem('女')
newItem.setFont(QFont('宋体',14,QFont.Black))
self.tablewidget.setItem(1,1,newItem)
newItem = QTableWidgetItem('160')
newItem.setFont(QFont('宋体',28,QFont.Black))
newItem.setForeground(QBrush(QColor(0,120,255)))
self.tablewidget.setItem(1,2,newItem)
newItem = QTableWidgetItem('太上老君')
newItem.setFont(QFont('微软雅黑',14,QFont.Black))
self.tablewidget.setItem(2,0,newItem)
newItem = QTableWidgetItem('男')
newItem.setFont(QFont('宋体',14,QFont.Black))
self.tablewidget.setItem(2,1,newItem)
newItem = QTableWidgetItem('170')
newItem.setFont(QFont('宋体',28,QFont.Black))
newItem.setForeground(QBrush(QColor(0,120,255)))
self.tablewidget.setItem(2,2,newItem)
newItem = QTableWidgetItem('如来佛祖')
newItem.setFont(QFont('微软雅黑',14,QFont.Black))
self.tablewidget.setItem(3,0,newItem)
newItem = QTableWidgetItem('男')
newItem.setFont(QFont('宋体',14,QFont.Black))
self.tablewidget.setItem(3,1,newItem)
newItem = QTableWidgetItem('190')
newItem.setFont(QFont('宋体',28,QFont.Black))
newItem.setForeground(QBrush(QColor(0,120,255)))
self.tablewidget.setItem(3,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 = QApplication(sys.argv)
# 设置应用图标
app.setWindowIcon(QIcon('../web.ico'))
w = ColumnSortDemo()
w.show()
sys.exit(app.exec_())