QTableWidget整数 [英] QTableWidget Integer

查看:362
本文介绍了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)

以下代码有效,但对于字符串only:

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)

响应:PyQt4 .QtCore.QVariant对象位于0x1f01fa60

Response: PyQt4.QtCore.QVariant object at 0x1f01fa60

或者这个:

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

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

回复:sip.voidptr对象位于0x1e904a80

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整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆