我想问一下,是否有一种方法可以检查项目是在QListWidget中还是在QComboBox中。这样,我想确保每一项在QListWidget或QCombobox中都是唯一的。如果可能的话,我想要这样的东西:
for i in range(0, len(LISTE), 1):
if LISTE[i] in self.liste:
return
else:
self.liste.addItem(LISTE[i])发布于 2019-08-07 21:54:23
您可以创建自己的QComboBox (和QListWidget)的子类。然后,您可以创建方法__contains__并重载其他方法。如下例所示:
from PyQt5.QtWidgets import QComboBox, QApplication
class UniqueComboBox(QComboBox):
def __init__(self):
super().__init__()
self._current_values = set()
def __contains__(self, item):
return item in self._current_values
def addItem(self, text): # this is simple implementation. do not support all cases
if text not in self._current_values:
super().addItem(text)
self._current_values.add(text)
def addItems(self, text_list):
for el in text_list:
self.addItem(el)
if __name__ == "__main__":
app = QApplication([])
combo = UniqueComboBox()
combo.addItems(["a", "b", "a", "c"])
print("a" in combo, "z" in combo)
combo.show()
app.exec_()https://stackoverflow.com/questions/57394770
复制相似问题