QTableWidget Integer

青春壹個敷衍的年華 提交于 2019-11-28 11:40:47

You were on the right track. Your code doesn't work because you're not calling the QTableWidgetItem's setData() function but trying to assign it a value. You have

item.setData = (Qt.DisplayRole,intValue)

instead of

item.setData(Qt.DisplayRole,intValue)

Also, when reading the data back it's not just the location that's shown but the data itself as a QVariant. You should find that item.data(Qt.DisplayRole).toString() will return your data back as a string by converting the QVariant (via its .toString() method).

Here's a quick working example just to demonstrate:

import sys
from PyQt4.QtGui import QApplication, QWidget, QTableWidget, QTableWidgetItem, QVBoxLayout
from PyQt4.QtCore import Qt

class Widget(QWidget):
    def __init__(self, parent=None):
        QWidget.__init__(self, parent)

        self.widget_layout = QVBoxLayout()
        self.table_widget = QTableWidget(101, 1)
        self.table_widget.setSortingEnabled(True)

        self.widget_layout.addWidget(self.table_widget)
        self.setLayout(self.widget_layout)

        for num in xrange(101):
            item = QTableWidgetItem()
            item.setData(Qt.EditRole, num)
            self.table_widget.setItem(num, 0, item)


if __name__ == '__main__':
  app = QApplication(sys.argv)
  widget = Widget()
  widget.show()
  sys.exit(app.exec_())
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!