Skip to main content
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

To do this you must create a new widget derived from the QTable, and set these constraits manually.

HereHere is an example how to sort columns:

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)

If you want to set decimal precision you must do this in similar way.

To do this you must create a new widget derived from the QTable, and set these constraits manually.

Here is an example how to sort columns:

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)

If you want to set decimal precision you must do this in similar way.

To do this you must create a new widget derived from the QTable, and set these constraits manually.

Here is an example how to sort columns:

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)

If you want to set decimal precision you must do this in similar way.

Source Link
dmh126
  • 6.8k
  • 2
  • 22
  • 36

To do this you must create a new widget derived from the QTable, and set these constraits manually.

Here is an example how to sort columns:

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)

If you want to set decimal precision you must do this in similar way.