比较对象并在列表中搜索
Compare objects and search in list
我使用 Python 3 和 PySide2(Qt for Python)(都是最新的)。我有一个 PySide2 对象列表,必须检查列表中是否存在某个项目。如果我尝试这样做,我会收到错误消息:
NotImplementedError: operator not implemented.
from PySide2 import QtGui
item = QtGui.QStandardItem()
item1 = QtGui.QStandardItem()
item == item1 # creates error
list1 = [item, item1]
item1 in list1 # creats error
我做错了什么?我怎样才能做到这一点?我必须自己实现“==”运算符吗?
预先感谢您的帮助!
运算符==
等价于__eq__
。用法类似于 a.__eq__(b)
或 a == b
。例外情况是 the class 没有实现该方法。
而且文档确实说:
Reimplement operator if you want to control the semantics of item comparison. operator determines the sorted order when sorting items with sortChildren()
or with sort()
.
如评论中所述,您得到的错误是 bug 的一部分,它是 PySide 的残余。
我认为你有一个 XY problem, and what you want is to check if there is an item with a predefined text. If so, it is not necessary to implement the operator ==
but use the findItems() 方法:
from PySide2 import QtCore, QtGui
if __name__ == "__main__":
import sys
md = QtGui.QStandardItemModel()
for text in ("Hello", "Stack", "Overflow"):
md.appendRow(QtGui.QStandardItem(text))
words = ("Hello", "World")
for word in words:
if md.findItems(word, flags=QtCore.Qt.MatchExactly, column=0):
print(f"{word} exists")
else:
print(f"{word} not exists")
如果您想搜索其他角色,请使用match()方法:
from PySide2 import QtCore, QtGui
FooRole = QtCore.Qt.UserRole + 1000
if __name__ == "__main__":
import sys
md = QtGui.QStandardItemModel()
for i, text in enumerate(("Hello", "Stack", "Overflow")):
it = QtGui.QStandardItem(str(i))
it.setData(text, FooRole)
md.appendRow(it)
words = ("Hello", "World")
for word in words:
if md.match(
md.index(0, 0), FooRole, word, hits=1, flags=QtCore.Qt.MatchExactly
):
print(f"{word} exists")
else:
print(f"{word} not exists")
我使用 Python 3 和 PySide2(Qt for Python)(都是最新的)。我有一个 PySide2 对象列表,必须检查列表中是否存在某个项目。如果我尝试这样做,我会收到错误消息:
NotImplementedError: operator not implemented.
from PySide2 import QtGui
item = QtGui.QStandardItem()
item1 = QtGui.QStandardItem()
item == item1 # creates error
list1 = [item, item1]
item1 in list1 # creats error
我做错了什么?我怎样才能做到这一点?我必须自己实现“==”运算符吗? 预先感谢您的帮助!
运算符==
等价于__eq__
。用法类似于 a.__eq__(b)
或 a == b
。例外情况是 the class 没有实现该方法。
而且文档确实说:
Reimplement operator if you want to control the semantics of item comparison. operator determines the sorted order when sorting items with
sortChildren()
or withsort()
.
如评论中所述,您得到的错误是 bug 的一部分,它是 PySide 的残余。
我认为你有一个 XY problem, and what you want is to check if there is an item with a predefined text. If so, it is not necessary to implement the operator ==
but use the findItems() 方法:
from PySide2 import QtCore, QtGui
if __name__ == "__main__":
import sys
md = QtGui.QStandardItemModel()
for text in ("Hello", "Stack", "Overflow"):
md.appendRow(QtGui.QStandardItem(text))
words = ("Hello", "World")
for word in words:
if md.findItems(word, flags=QtCore.Qt.MatchExactly, column=0):
print(f"{word} exists")
else:
print(f"{word} not exists")
如果您想搜索其他角色,请使用match()方法:
from PySide2 import QtCore, QtGui
FooRole = QtCore.Qt.UserRole + 1000
if __name__ == "__main__":
import sys
md = QtGui.QStandardItemModel()
for i, text in enumerate(("Hello", "Stack", "Overflow")):
it = QtGui.QStandardItem(str(i))
it.setData(text, FooRole)
md.appendRow(it)
words = ("Hello", "World")
for word in words:
if md.match(
md.index(0, 0), FooRole, word, hits=1, flags=QtCore.Qt.MatchExactly
):
print(f"{word} exists")
else:
print(f"{word} not exists")