title: sort a column with setCellWidget in QTableWidget
body: <p>I am trying to sort a column which has progressbar in it. I want column to be sorted by progressbar value. Any help will be much appreciated
  <a href="https://i.stack.imgur.com/z3Cwi.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/z3Cwi.png" alt="enter image description here"></a></p>
  
  <pre><code>from PyQt4.QtCore import *
  from PyQt4.QtGui import *
  import sys
  
  
  class Example(QMainWindow):
  
      def __init__(self):
          super(Example, self).__init__()
  
          self.initUI()
  
      def initUI(self):
  
          self.tablew = Table(self)
          self.setCentralWidget(self.tablew)
  
          self.setGeometry(300, 300, 300, 200)
          self.show()
  
  
  class ProgressBar(QProgressBar):
  
      def __init__(self, value, parent=None):
          QProgressBar.__init__(self)
          self.setMinimum(1)
          self.setMaximum(5.0)
          self.setValue(value)
          self.setFormat('{0:.5f}'.format(value))
          style = ''' QProgressBar{max-height: 15px;text-align: center;}'''
          self.setStyleSheet(style)
  
  
  class Table(QTableWidget):
  
      def __init__(self,   parent=None):
          QTableWidget.__init__(self)
          self.setColumnCount(2)
          self.setHorizontalHeaderLabels(['id', 'status'])
          header = self.horizontalHeader()
          header.setResizeMode(QHeaderView.Stretch)
          self.setSortingEnabled(True)
          self.loadData()
          #
  
      def loadData(self):
          #
          tableData = [[89, 4.8], [99, 3.9], [101, 2.6], [105, 4]]
          #
          self.setRowCount(len(tableData))
          #
          for index, value in enumerate(tableData):
              # column1
              item = QTableWidgetItem()
              item.setData(Qt.DisplayRole, value[0])
              self.setItem(index, 0, item)
              # column2
              self.setCellWidget(index, 1, ProgressBar(value[1]))
          #
  
  
  def main():
  
      app = QApplication(sys.argv)
      ex = Example()
      sys.exit(app.exec_())
  
  
  if __name__ == '__main__':
      main()
  </code></pre>
  
  <p><strong>UPDATE:</strong>
  Based on answer from @ekhumoro 
  Below is the complete code but somehow it doest sort correctly.
  Screenshot attached after sorting column manually
  <a href="https://i.stack.imgur.com/ANplc.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ANplc.png" alt="probelm in sorting"></a></p>
  
  <pre><code>from PyQt4.QtCore import *
  from PyQt4.QtGui import *
  import sys
  
  
  class Example(QMainWindow):
  
      def __init__(self):
          super(Example, self).__init__()
  
          self.initUI()
  
      def initUI(self):
  
          self.tablew = Table(self)
          self.setCentralWidget(self.tablew)
  
          self.setGeometry(300, 300, 300, 200)
          self.show()
  
  
  class ProgressBar(QProgressBar):
  
      def __init__(self, value, parent=None):
          QProgressBar.__init__(self)
          self.setMinimum(1)
          self.setMaximum(5.0)
          self.setValue(value)
          self.setFormat('{0:.5f}'.format(value))
          style = ''' QProgressBar{max-height: 15px;text-align: center;}'''
          self.setStyleSheet(style)
  
  
  class Table(QTableWidget):
  
      def __init__(self,   parent=None):
          QTableWidget.__init__(self)
          self.setColumnCount(2)
          self.setHorizontalHeaderLabels(['id', 'status'])
          header = self.horizontalHeader()
          header.setResizeMode(QHeaderView.Stretch)
          self.setSortingEnabled(True)
          self.loadData()
          #
  
      def loadData(self):
          #
          tableData = [[89, 4.8], [99, 3.9], [101, 2.6], [105, 4]]
          #
          self.setRowCount(len(tableData))
          #
          for index, value in enumerate(tableData):
              # column1
              item = QTableWidgetItem()
              item.setData(Qt.DisplayRole, value[0])
              self.setItem(index, 0, item)
              # column2
              item = ProgressWidgetItem()
              self.setItem(index, 1, item)
              item.updateValue(value[1])
              progress = ProgressBar(value[1])
              self.setCellWidget(index, 1, progress)
              progress.valueChanged.connect(item.updateValue)
          self.sortItems(1, Qt.DescendingOrder)
  
  
  class ProgressWidgetItem(QTableWidgetItem):
  
      def __lt__(self, other):
          return self.data(Qt.UserRole) &lt; other.data(Qt.UserRole)
  
      def updateValue(self, value):
          self.setData(Qt.UserRole, value)
  
  
  def main():
  
      app = QApplication(sys.argv)
      ex = Example()
      sys.exit(app.exec_())
  
  
  if __name__ == '__main__':
      main()
  </code></pre>