【发布时间】:2016-03-05 14:51:25
【问题描述】:
我将一个对象作为 UserData 附加到一个 QStandardItem,该对象被添加到一个 QComboBox 模型中。如果我然后用 findData() 方法搜索它,我不会得到任何结果。如果我对一个简单的 int 做同样的事情,我会得到一个结果。我想这是 PySide 相关的,但我在源代码中找不到包装器。这是一个(有点)最小的例子:
import sys
from PySide import QtGui
class Foo(object):
def __init__(self, value):
self.value = value
class MyCombo(QtGui.QWidget):
def __init__(self, *args):
QtGui.QWidget.__init__(self, *args)
combo = QtGui.QComboBox()
combo.addItem(str(1), Foo(1))
combo.addItem(str(2), 2)
data = combo.itemData(0)
print(data) # prints the object repr
print(combo.findData(data)) # returns -1 -> not found
data = combo.itemData(1)
print(data) # prints 2
print(combo.findData(data)) # returns 1 -> found
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
w = MyCombo()
w.show()
sys.exit(app.exec_())
为什么 findData() 为对象返回 -1?任何可以搜索的提示都表示赞赏!
【问题讨论】:
标签: pyside python-3.4 qcombobox