本文整理汇总了Python中UM.Qt.ListModel.ListModel.setItems方法的典型用法代码示例。如果您正苦于以下问题:Python ListModel.setItems方法的具体用法?Python ListModel.setItems怎么用?Python ListModel.setItems使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类UM.Qt.ListModel.ListModel
的用法示例。
在下文中一共展示了ListModel.setItems方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: TestListModel
# 需要导入模块: from UM.Qt.ListModel import ListModel [as 别名]
# 或者: from UM.Qt.ListModel.ListModel import setItems [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}
示例2: PluginBrowser
# 需要导入模块: from UM.Qt.ListModel import ListModel [as 别名]
# 或者: from UM.Qt.ListModel.ListModel import setItems [as 别名]
#.........这里部分代码省略.........
self.setIsDownloading(False)
self._download_plugin_reply.downloadProgress.disconnect(self._onDownloadPluginProgress)
self._temp_plugin_file = tempfile.NamedTemporaryFile(suffix = ".curaplugin")
self._temp_plugin_file.write(self._download_plugin_reply.readAll())
result = PluginRegistry.getInstance().installPlugin("file://" + self._temp_plugin_file.name)
self._newly_installed_plugin_ids.append(result["id"])
self.pluginsMetadataChanged.emit()
Application.getInstance().messageBox(i18n_catalog.i18nc("@window:title", "Plugin browser"), result["message"])
self._temp_plugin_file.close() # Plugin was installed, delete temp file
@pyqtProperty(int, notify = onDownloadProgressChanged)
def downloadProgress(self):
return self._download_progress
@pyqtSlot(str)
def downloadAndInstallPlugin(self, url):
Logger.log("i", "Attempting to download & install plugin from %s", url)
url = QUrl(url)
self._download_plugin_request = QNetworkRequest(url)
self._download_plugin_request.setRawHeader(*self._request_header)
self._download_plugin_reply = self._network_manager.get(self._download_plugin_request)
self._download_progress = 0
self.setIsDownloading(True)
self.onDownloadProgressChanged.emit()
self._download_plugin_reply.downloadProgress.connect(self._onDownloadPluginProgress)
@pyqtProperty(QObject, notify=pluginsMetadataChanged)
def pluginsModel(self):
if self._plugins_model is None:
self._plugins_model = ListModel()
self._plugins_model.addRoleName(Qt.UserRole + 1, "name")
self._plugins_model.addRoleName(Qt.UserRole + 2, "version")
self._plugins_model.addRoleName(Qt.UserRole + 3, "short_description")
self._plugins_model.addRoleName(Qt.UserRole + 4, "author")
self._plugins_model.addRoleName(Qt.UserRole + 5, "already_installed")
self._plugins_model.addRoleName(Qt.UserRole + 6, "file_location")
self._plugins_model.addRoleName(Qt.UserRole + 7, "can_upgrade")
else:
self._plugins_model.clear()
items = []
for metadata in self._plugins_metadata:
items.append({
"name": metadata["label"],
"version": metadata["version"],
"short_description": metadata["short_description"],
"author": metadata["author"],
"already_installed": self._checkAlreadyInstalled(metadata["id"]),
"file_location": metadata["file_location"],
"can_upgrade": self._checkCanUpgrade(metadata["id"], metadata["version"])
})
self._plugins_model.setItems(items)
return self._plugins_model
def _checkCanUpgrade(self, id, version):
plugin_registry = PluginRegistry.getInstance()
metadata = plugin_registry.getMetaData(id)
if metadata != {}:
if id in self._newly_installed_plugin_ids:
return False # We already updated this plugin.
current_version = Version(metadata["plugin"]["version"])
new_version = Version(version)
if new_version > current_version:
return True
return False
def _checkAlreadyInstalled(self, id):
plugin_registry = PluginRegistry.getInstance()
metadata = plugin_registry.getMetaData(id)
if metadata != {}:
return True
else:
if id in self._newly_installed_plugin_ids:
return True # We already installed this plugin, but the registry just doesn't know it yet.
return False
def _onRequestFinished(self, reply):
reply_url = reply.url().toString()
if reply.operation() == QNetworkAccessManager.GetOperation:
if reply_url == self._api_url + "plugins":
try:
json_data = json.loads(bytes(reply.readAll()).decode("utf-8"))
self._plugins_metadata = json_data
self.pluginsMetadataChanged.emit()
except json.decoder.JSONDecodeError:
Logger.log("w", "Received an invalid print job state message: Not valid JSON.")
return
else:
# Ignore any operation that is not a get operation
pass
def _createNetworkManager(self):
if self._network_manager:
self._network_manager.finished.disconnect(self._onRequestFinished)
self._network_manager = QNetworkAccessManager()
self._network_manager.finished.connect(self._onRequestFinished)