Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 对QTableWidget项进行数字排序_Python_Sorting_Python 2.7_Pyqt4_Qtablewidget - Fatal编程技术网

Python 对QTableWidget项进行数字排序

Python 对QTableWidget项进行数字排序,python,sorting,python-2.7,pyqt4,qtablewidget,Python,Sorting,Python 2.7,Pyqt4,Qtablewidget,当启用排序时,在PyQt4中使用QTableWidget,将项目排序为字符串 变量=[1,2,3,4,5,11,22,33] 产生秩序 一, 11 2. 22 3. 33 4. 五, 我目前正在使用下面的代码填写表格 tableWidgetData.setItem(0, 0, QtGui.QTableWidgetItem(variable)) 我已经尝试过了,因为我认为变量只能按字符串排序,因为它们是字符串 tableWidgetData.setItem(0, 0, QtGui.QTableW

当启用排序时,在PyQt4中使用QTableWidget,将项目排序为字符串

变量=[1,2,3,4,5,11,22,33]

产生秩序

一, 11 2. 22 3. 33 4. 五,

我目前正在使用下面的代码填写表格

tableWidgetData.setItem(0, 0, QtGui.QTableWidgetItem(variable))
我已经尝试过了,因为我认为变量只能按字符串排序,因为它们是字符串

tableWidgetData.setItem(0, 0, QtGui.QTableWidgetItem(int(variable)))

但这是不可能的。我哪里出错了?

如果有任何东西在
QtGui.QTableWidgetItem
构造函数中传递变量,我必须是
QtCore.QString
ot python sring

要修复此问题,请创建自定义的
QtGui.QTableWidgetItem
,并通过overide实现check case-less-than(或者我们在python中知道的是)

榜样

import sys
import random
from PyQt4 import QtCore, QtGui

class QCustomTableWidgetItem (QtGui.QTableWidgetItem):
    def __init__ (self, value):
        super(QCustomTableWidgetItem, self).__init__(QtCore.QString('%s' % value))

    def __lt__ (self, other):
        if (isinstance(other, QCustomTableWidgetItem)):
            selfDataValue  = float(self.data(QtCore.Qt.EditRole).toString())
            otherDataValue = float(other.data(QtCore.Qt.EditRole).toString())
            return selfDataValue < otherDataValue
        else:
            return QtGui.QTableWidgetItem.__lt__(self, other)

class QCustomTableWidget (QtGui.QTableWidget):
    def __init__ (self, parent = None):
        super(QCustomTableWidget, self).__init__(parent)
        self.setColumnCount(2)
        self.setRowCount(5)
        for row in range(self.rowCount()):
            self.setItem(row, 0, QCustomTableWidgetItem(random.random() * 1e4))
            self.setItem(row, 1, QtGui.QTableWidgetItem(QtCore.QString(65 + row)))
        self.setSortingEnabled(True)

myQApplication = QtGui.QApplication([])
myQCustomTableWidget = QCustomTableWidget()
myQCustomTableWidget.show()
sys.exit(myQApplication.exec_())
导入系统 随机输入 从PyQt4导入QtCore、QtGui 类QCustomTableWidgetItem(QtGui.QTableWidgetItem): 定义初始值(自身,值): super(QCustomTableWidgetItem,self)。\uuuu初始化(QtCore.QString(“%s”%value)) 定义(自身、其他): 如果(isinstance(其他,QCustomTableWidgetItem)): selfDataValue=float(self.data(QtCore.Qt.EditRole.toString()) otherDataValue=float(other.data(QtCore.Qt.EditRole.toString()) 返回selfDataValue如果有任何东西在
QtGui.QTableWidgetItem
构造函数中传递变量,则我必须是
QtCore.QString
ot python sring

要修复此问题,请创建自定义的
QtGui.QTableWidgetItem
,并通过overide实现check case-less-than(或者我们在python中知道的是)

榜样

import sys
import random
from PyQt4 import QtCore, QtGui

class QCustomTableWidgetItem (QtGui.QTableWidgetItem):
    def __init__ (self, value):
        super(QCustomTableWidgetItem, self).__init__(QtCore.QString('%s' % value))

    def __lt__ (self, other):
        if (isinstance(other, QCustomTableWidgetItem)):
            selfDataValue  = float(self.data(QtCore.Qt.EditRole).toString())
            otherDataValue = float(other.data(QtCore.Qt.EditRole).toString())
            return selfDataValue < otherDataValue
        else:
            return QtGui.QTableWidgetItem.__lt__(self, other)

class QCustomTableWidget (QtGui.QTableWidget):
    def __init__ (self, parent = None):
        super(QCustomTableWidget, self).__init__(parent)
        self.setColumnCount(2)
        self.setRowCount(5)
        for row in range(self.rowCount()):
            self.setItem(row, 0, QCustomTableWidgetItem(random.random() * 1e4))
            self.setItem(row, 1, QtGui.QTableWidgetItem(QtCore.QString(65 + row)))
        self.setSortingEnabled(True)

myQApplication = QtGui.QApplication([])
myQCustomTableWidget = QCustomTableWidget()
myQCustomTableWidget.show()
sys.exit(myQApplication.exec_())
导入系统 随机输入 从PyQt4导入QtCore、QtGui 类QCustomTableWidgetItem(QtGui.QTableWidgetItem): 定义初始值(自身,值): super(QCustomTableWidgetItem,self)。\uuuu初始化(QtCore.QString(“%s”%value)) 定义(自身、其他): 如果(isinstance(其他,QCustomTableWidgetItem)): selfDataValue=float(self.data(QtCore.Qt.EditRole.toString()) otherDataValue=float(other.data(QtCore.Qt.EditRole.toString()) 返回selfDataValue如果有任何东西在
QtGui.QTableWidgetItem
构造函数中传递变量,则我必须是
QtCore.QString
ot python sring

要修复此问题,请创建自定义的
QtGui.QTableWidgetItem
,并通过overide实现check case-less-than(或者我们在python中知道的是)

榜样

import sys
import random
from PyQt4 import QtCore, QtGui

class QCustomTableWidgetItem (QtGui.QTableWidgetItem):
    def __init__ (self, value):
        super(QCustomTableWidgetItem, self).__init__(QtCore.QString('%s' % value))

    def __lt__ (self, other):
        if (isinstance(other, QCustomTableWidgetItem)):
            selfDataValue  = float(self.data(QtCore.Qt.EditRole).toString())
            otherDataValue = float(other.data(QtCore.Qt.EditRole).toString())
            return selfDataValue < otherDataValue
        else:
            return QtGui.QTableWidgetItem.__lt__(self, other)

class QCustomTableWidget (QtGui.QTableWidget):
    def __init__ (self, parent = None):
        super(QCustomTableWidget, self).__init__(parent)
        self.setColumnCount(2)
        self.setRowCount(5)
        for row in range(self.rowCount()):
            self.setItem(row, 0, QCustomTableWidgetItem(random.random() * 1e4))
            self.setItem(row, 1, QtGui.QTableWidgetItem(QtCore.QString(65 + row)))
        self.setSortingEnabled(True)

myQApplication = QtGui.QApplication([])
myQCustomTableWidget = QCustomTableWidget()
myQCustomTableWidget.show()
sys.exit(myQApplication.exec_())
导入系统 随机输入 从PyQt4导入QtCore、QtGui 类QCustomTableWidgetItem(QtGui.QTableWidgetItem): 定义初始值(自身,值): super(QCustomTableWidgetItem,self)。\uuuu初始化(QtCore.QString(“%s”%value)) 定义(自身、其他): 如果(isinstance(其他,QCustomTableWidgetItem)): selfDataValue=float(self.data(QtCore.Qt.EditRole.toString()) otherDataValue=float(other.data(QtCore.Qt.EditRole.toString()) 返回selfDataValue如果有任何东西在
QtGui.QTableWidgetItem
构造函数中传递变量,则我必须是
QtCore.QString
ot python sring

要修复此问题,请创建自定义的
QtGui.QTableWidgetItem
,并通过overide实现check case-less-than(或者我们在python中知道的是)

榜样

import sys
import random
from PyQt4 import QtCore, QtGui

class QCustomTableWidgetItem (QtGui.QTableWidgetItem):
    def __init__ (self, value):
        super(QCustomTableWidgetItem, self).__init__(QtCore.QString('%s' % value))

    def __lt__ (self, other):
        if (isinstance(other, QCustomTableWidgetItem)):
            selfDataValue  = float(self.data(QtCore.Qt.EditRole).toString())
            otherDataValue = float(other.data(QtCore.Qt.EditRole).toString())
            return selfDataValue < otherDataValue
        else:
            return QtGui.QTableWidgetItem.__lt__(self, other)

class QCustomTableWidget (QtGui.QTableWidget):
    def __init__ (self, parent = None):
        super(QCustomTableWidget, self).__init__(parent)
        self.setColumnCount(2)
        self.setRowCount(5)
        for row in range(self.rowCount()):
            self.setItem(row, 0, QCustomTableWidgetItem(random.random() * 1e4))
            self.setItem(row, 1, QtGui.QTableWidgetItem(QtCore.QString(65 + row)))
        self.setSortingEnabled(True)

myQApplication = QtGui.QApplication([])
myQCustomTableWidget = QCustomTableWidget()
myQCustomTableWidget.show()
sys.exit(myQApplication.exec_())
导入系统 随机输入 从PyQt4导入QtCore、QtGui 类QCustomTableWidgetItem(QtGui.QTableWidgetItem): 定义初始值(自身,值): super(QCustomTableWidgetItem,self)。\uuuu初始化(QtCore.QString(“%s”%value)) 定义(自身、其他): 如果(isinstance(其他,QCustomTableWidgetItem)): selfDataValue=float(self.data(QtCore.Qt.EditRole.toString()) otherDataValue=float(other.data(QtCore.Qt.EditRole.toString()) 返回selfDataValue