本文整理汇总了Python中UM.Qt.ListModel.ListModel.sort方法的典型用法代码示例。如果您正苦于以下问题:Python ListModel.sort方法的具体用法?Python ListModel.sort怎么用?Python ListModel.sort使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类UM.Qt.ListModel.ListModel
的用法示例。
在下文中一共展示了ListModel.sort方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: createOptionsModel
# 需要导入模块: from UM.Qt.ListModel import ListModel [as 别名]
# 或者: from UM.Qt.ListModel.ListModel import sort [as 别名]
def createOptionsModel(self, options):
if not options:
return None
model = ListModel()
model.addRoleName(Qt.UserRole + 1, "value")
model.addRoleName(Qt.UserRole + 2, "name")
for value, name in options.items():
model.appendItem({"value": str(value), "name": str(name)})
model.sort(lambda t: t["name"])
return model
示例2: TestListModel
# 需要导入模块: from UM.Qt.ListModel import ListModel [as 别名]
# 或者: from UM.Qt.ListModel.ListModel import sort [as 别名]
class TestListModel(TestCase):
list_model = None # type: ListModel
test_data = [{"name": "yay", "data": 12}, {"name": "omg", "data": 13}, {"name":"zomg", "data": 14}]
NameRole = Qt.UserRole + 1
DataRole = Qt.UserRole + 2
def setUp(self):
self.list_model = ListModel()
self.list_model.addRoleName(self.NameRole, "name")
self.list_model.addRoleName(self.DataRole, "data")
self.list_model.setItems(deepcopy(self.test_data))
def test_getItem(self):
assert self.list_model.getItem(0) == {"name": "yay", "data": 12}
assert self.list_model.getItem(9001) == {}
def test_items(self):
assert self.list_model.items == self.test_data
def test_insertItem(self):
self.list_model.insertItem(0, {"name": "zomg!", "data": "yay"})
assert self.list_model.getItem(0) == {"name": "zomg!", "data": "yay"}
# Check if the previously first item is now the second one.
assert self.list_model.getItem(1) == {"name": "yay", "data": 12}
def test_removeItem(self):
self.list_model.removeItem(1)
print(self.list_model._items)
assert self.list_model.getItem(1) == {"name":"zomg", "data": 14}
def test_clear(self):
assert self.list_model.count == 3
self.list_model.clear()
assert self.list_model.count == 0
def test_appendItem(self):
self.list_model.appendItem({"name":"!", "data": 9001})
assert self.list_model.count == 4
assert self.list_model.getItem(3) == {"name":"!", "data": 9001}
def test_setProperty(self):
self.list_model.setProperty(0, "name", "new_data")
assert self.list_model.getItem(0)["name"] == "new_data"
def test_find(self):
assert self.list_model.find("name", "omg") == 1
assert self.list_model.find("data", 13) == 1
assert self.list_model.find("name", "zomg") == 2
assert self.list_model.find("name", "UNKNOWN") == -1
def test_setItems(self):
self.list_model.setItems([{"name": "zomg!", "data": "yay"}])
assert self.list_model.items == [{"name": "zomg!", "data": "yay"}]
def test_sort(self):
self.list_model.sort(lambda i: -i["data"])
assert self.list_model.getItem(0) == {"name":"zomg", "data": 14}
assert self.list_model.getItem(2) == {"name": "yay", "data": 12}