QTableWidget display certain decimals like excel

血红的双手。 提交于 2020-05-09 05:31:09

问题


My question is about the way the QTableWidget displays the cell values.

I'd like for the cell to show only three decimals when it's not being edited and show the full value when you double click for editing.


I am doing calculations in background and then setting the cell value afterwards.

V_D = 3/0.7
self.TableWidget.setItem(0, 0, QTableWidgetItem(str(V_D)))

Similar to the way excel formats the cell to show certain number of digits.

Full Value:

Display Value:

How would I go about doing this?


回答1:


The solution is to use a delegate and override the paint method that is responsible for showing what looks in normal state, I have built the following class based on the above.

class FloatDelegate(QItemDelegate):
    def __init__(self, decimals, parent=None):
        QItemDelegate.__init__(self, parent=parent)
        self.nDecimals = decimals

    def paint(self, painter, option, index):
        value = index.model().data(index, Qt.EditRole)
        try:
            number = float(value)
            painter.drawText(option.rect, Qt.AlignLeft, "{:.{}f}".format(number, self.nDecimals))
        except :
            QItemDelegate.paint(self, painter, option, index)

In your case you should use it as follows:

self.TableWidget.setItemDelegate(FloatDelegate(3))

Example:

if __name__ == '__main__':
    import sys
    app = QApplication(sys.argv)
    w = QTableWidget()
    w.setColumnCount(8)
    w.setRowCount(8)
    for i in range(w.rowCount()):
        for j in range(w.columnCount()):
            number = (i+1)/(j+1)
            w.setItem(i, j, QTableWidgetItem(str(number)))
    w.setItemDelegate(FloatDelegate(3, w))
    w.show()
    sys.exit(app.exec_())

Screenshots:

Plus:

By Row o Column:

#only column 2
setItemDelegateForColumn(2, FloatDelegate(3))
#only row 2
setItemDelegateForRow(2, FloatDelegate(3))

If you want to apply only to cell 2,3

def paint(self, painter, option, index):
   if index.row() == 2 and index.column() == 3:
        value = index.model().data(index, Qt.EditRole)
        try:
            number = float(value)
            painter.drawText(option.rect, Qt.AlignLeft, "{:.{}f}".format(number, self.nDecimals))
        except :
            QItemDelegate.paint(self, painter, option, index)
    else:
        QItemDelegate.paint(self, painter, option, index)

Update:

  • QStyledItemDelegate:

    class FloatDelegate(QStyledItemDelegate):
        def __init__(self, decimals, parent=None):
            super(FloatDelegate, self).__init__(parent=parent)
            self.nDecimals = decimals
    
        def displayText(self, value, locale):
            try:
                number = float(value)
            except ValueError:
                return super(FloatDelegate, self).displayText(value, locale)
            else:
                return locale.toString(number, format="f", precision=self.nDecimals)
    


来源:https://stackoverflow.com/questions/45599857/qtablewidget-display-certain-decimals-like-excel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!