Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
220 views
in Technique[技术] by (71.8m points)

python - QTableWidget Integer

I am trying to insert and display integers in my QTableWidget. They don't display. It works if I convert everything to strings, but then I can't sort columns numerically--only lexically (1, 10, 100, etc.). This is using PyQt.

I've tried some of the suggested solutions, using QTableWidgetItem.setData(someRole,intValue), bu then nothing at all displays. I've tried, Qt.UserRole, DisplayRole and Edit Role. (I don't understand why these Roles are needed to display integers, but have just followed the examples). My specific code is:

    item = QTableWidgetItem()
    item.setData = (Qt.DisplayRole,intValue)
    myTable.setItem(row, column, item)

The following code works, but for strings only:

    item = QTableWidgetItem(str(intValue))
    myTable.setItem(row, column, item)

Also, the suggestions for reading the data back, only show the object location, not the actual data. Example, using Eric as an interpreter shell:

item.data(Qt.DisplayRole)

Response: PyQt4.QtCore.QVariant object at 0x1f01fa60

or this:

item.data(Qt.EditRole).data()

Response: sip.voidptr object at 0x1e904a80

Any insight is appreciated.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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_())

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...