当前位置: 首页>>代码示例>>Python>>正文


Python ListModel.sort方法代码示例

本文整理汇总了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
开发者ID:Kiddo3D,项目名称:Uranium,代码行数:13,代码来源:SettingsFromCategoryModel.py

示例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}
开发者ID:Ultimaker,项目名称:Uranium,代码行数:65,代码来源:TestListModel.py


注:本文中的UM.Qt.ListModel.ListModel.sort方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。