def initUI(self): self.setViewMode(QListView.IconMode) for item in self.menus: widgetItem = QListWidgetItem(self) widgetItem.metadata = item widgetItem.setText(item['title']) widgetItem.setToolTip(item.get('description', '')) widgetItem.setIcon(QIcon(item['icon'])) self.addItem(widgetItem) self.setCurrentRow(0) self.setIconSize(QSize(32, 32)) self.setMovement(QListView.Static) self.setUniformItemSizes(True) # self.setSize() self.setStyleSheet("""QListWidget::Item { padding-left: 10px; padding-right: 10px; padding-bottom: 5px; }""") shadow = QGraphicsDropShadowEffect(self) shadow.setColor(Qt.black) shadow.setBlurRadius(10) shadow.setOffset(0, 0) self.setGraphicsEffect(shadow)
def reloadMenus(self, menus): self.menuList.clear() for menu in menus: listItem = QListWidgetItem(QIcon(menu['icon']), menu['title']) # 可以通过setData(self, int, Any)储存数据,通过data(self, int) -> Any获取数据 # 经过测试发现前几个索引返回的都是对象的内部属性,改用.属性直接赋值的方式储存数据 # listItem.setData(0, item) listItem.metadata = menu self.menuList.addItem(listItem) self.menuList.setCurrentRow(0)