QTableWidget Integer(QTableWidget 整数)
问题描述
我正在尝试在 QTableWidget 中插入和显示整数.他们不显示.如果我将所有内容都转换为字符串,它就可以工作,但是我不能按数字对列进行排序——只能按词法(1、10、100 等)进行排序.这是使用 PyQt.
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.
我尝试了一些建议的解决方案,使用 QTableWidgetItem.setData(someRole,intValue),然后什么也没有显示.我尝试过 Qt.UserRole、DisplayRole 和 Edit Role.(我不明白为什么需要这些角色来显示整数,但只是按照示例进行操作).我的具体代码是:
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)
另外,读回数据的建议,只显示对象位置,不显示实际数据.示例,使用 Eric 作为解释器 shell:
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)
item.data(Qt.DisplayRole)
响应:0x1f01fa60 处的 PyQt4.QtCore.QVariant 对象
Response: PyQt4.QtCore.QVariant object at 0x1f01fa60
或者这个:
item.data(Qt.EditRole).data()
item.data(Qt.EditRole).data()
响应:0x1e904a80 处的 sip.voidptr 对象
Response: sip.voidptr object at 0x1e904a80
感谢任何见解.
推荐答案
您走对了.您的代码不起作用,因为您没有调用 QTableWidgetItem 的 setData() 函数,而是尝试为其分配一个值.你有
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)
代替
item.setData(Qt.DisplayRole,intValue)
此外,在读取数据时,不仅显示的位置,而且数据本身作为 QVariant.您应该会发现 item.data(Qt.DisplayRole).toString() 将通过转换 QVariant(通过其 .toString() 方法).
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_())
这篇关于QTableWidget 整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:QTableWidget 整数
基础教程推荐
- 包装空间模型 2022-01-01
- 在同一图形上绘制Bokeh的烛台和音量条 2022-01-01
- 无法导入 Pytorch [WinError 126] 找不到指定的模块 2022-01-01
- 使用大型矩阵时禁止 Pycharm 输出中的自动换行符 2022-01-01
- PANDA VALUE_COUNTS包含GROUP BY之前的所有值 2022-01-01
- PermissionError: pip 从 8.1.1 升级到 8.1.2 2022-01-01
- 修改列表中的数据帧不起作用 2022-01-01
- Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙? 2022-01-01
- 求两个直方图的卷积 2022-01-01
- 在Python中从Azure BLOB存储中读取文件 2022-01-01
