栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在pyqt tablewidget中排序

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在pyqt tablewidget中排序

它按字母数字排序(因此,按照字符串,“ 1”,“ 10”,“ 11”,“ 12”,“ 2”,“ 20”,“ 21”,“ 22”,“ 3”,“
4’等是正确的排序顺序。对于QTableWidgetItem,如果您使用setData(Qt.EditRole,value)方法,则该排序顺序将起作用。根据您的Qt版本(我假设),您可能拥有重载表格小部件项的小于方法。

from PyQt4.QtCore import Qt, QVariantfrom PyQt4.QtGui import QApplication, QTableWidget, QTableWidgetItemclass MyTableWidgetItem(QTableWidgetItem):    def __lt__(self, other):        if ( isinstance(other, QTableWidgetItem) ): my_value, my_ok = self.data(Qt.EditRole).toInt() other_value, other_ok = other.data(Qt.EditRole).toInt() if ( my_ok and other_ok ):     return my_value < other_value        return super(MyTableWidgetItem, self).__lt__(other)if ( __name__ == '__main__' ):    app = None    if ( QApplication.instance() is None ):        app = QApplication([])    widget = QTableWidget()    widget.setWindowFlags(Qt.Dialog)    widget.setSortingEnabled(True)    widget.setRowCount(50)    widget.setColumnCount(3)    for row in range(50):       # create a normal QTableWidgetItem       a = QTableWidgetItem()       a.setText(str(row))       widget.setItem(row, 0, a)       # create a proper sorted item       b = QTableWidgetItem()       b.setData(Qt.EditRole, QVariant(row))       widget.setItem(row, 1, b)       # create a custom sorted item       c = MyTableWidgetItem()       c.setData(Qt.EditRole, QVariant(row))       widget.setItem(row, 2, c)    widget.show()    if ( app ):        app.exec_()


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/376388.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号