diff --git a/qfluentwidgets/__init__.py b/qfluentwidgets/__init__.py index e390a64de0b52ce1a882b5aa998aaad222453475..0e984978659838f7cc38f1c69160bf2e72ef49d9 100644 --- a/qfluentwidgets/__init__.py +++ b/qfluentwidgets/__init__.py @@ -12,7 +12,7 @@ Examples are available at https://github.com/zhiyiYo/PyQt-Fluent-Widgets/tree/ma :license: GPLv3, see LICENSE for more details. """ -__version__ = "0.6.5" +__version__ = "0.6.6" from .components import * from .common import * diff --git a/qfluentwidgets/components/widgets/combo_box.py b/qfluentwidgets/components/widgets/combo_box.py index 0ec3124e8949a867dbc10e7c7800fba02f0a8607..1604461ee00812597f9dd466dde754fd0316389e 100644 --- a/qfluentwidgets/components/widgets/combo_box.py +++ b/qfluentwidgets/components/widgets/combo_box.py @@ -31,10 +31,7 @@ class ComboItem: """ self.text = text self.userData = userData - if icon: - self._icon = QIcon(icon) if isinstance(icon, str) else icon - else: - self._icon = QIcon() + self.icon = icon @property def icon(self): @@ -43,6 +40,13 @@ class ComboItem: return self._icon.icon() + @icon.setter + def icon(self, ico: Union[str, QIcon, FluentIconBase]): + if ico: + self._icon = QIcon(ico) if isinstance(ico, str) else ico + else: + self._icon = QIcon() + class ComboBoxBase(QObject): """ Combo box base """ @@ -75,7 +79,7 @@ class ComboBoxBase(QObject): return super().eventFilter(obj, e) - def addItem(self, text, icon: Union[str, QIcon, FluentIconBase] = None, userData=None): + def addItem(self, text: str, icon: Union[str, QIcon, FluentIconBase] = None, userData=None): """ add item Parameters @@ -127,7 +131,7 @@ class ComboBoxBase(QObject): def currentIndex(self): return self._currentIndex - def setCurrentIndex(self, index): + def setCurrentIndex(self, index: int): """ set current index Parameters @@ -151,6 +155,12 @@ class ComboBoxBase(QObject): return self.items[self.currentIndex()].text + def currentData(self): + if not 0 <= self.currentIndex() < len(self.items): + return None + + return self.items[self.currentIndex()].userData + def setCurrentText(self, text): """ set the current text displayed in combo box, text should be in the item list @@ -186,17 +196,36 @@ class ComboBoxBase(QObject): self.setText(text) def itemData(self, index: int): - """ Returns the data for the given role in the given index in the combobox """ + """ Returns the data in the given index """ if not 0 <= index < len(self.items): return None return self.items[index].userData + def itemText(self, index: int): + """ Returns the text in the given index """ + if not 0 <= index < len(self.items): + return '' + + return self.items[index].text + + def itemIcon(self, index: int): + """ Returns the icon in the given index """ + if not 0 <= index < len(self.items): + return QIcon() + + return self.items[index].icon + def setItemData(self, index: int, value): - """ Sets the data role for the item on the given index in the combobox to the specified value """ + """ Sets the data role for the item on the given index """ if 0 <= index < len(self.items): self.items[index].userData = value + def setItemIcon(self, index: int, icon: Union[str, QIcon, FluentIconBase]): + """ Sets the data role for the item on the given index """ + if 0 <= index < len(self.items): + self.items[index].icon = icon + def findData(self, data): """ Returns the index of the item containing the given data, otherwise returns -1 """ for i, item in enumerate(self.items): @@ -212,6 +241,43 @@ class ComboBoxBase(QObject): return self.items.index(self.itemMap[text]) + def clear(self): + """ Clears the combobox, removing all items. """ + self.items.clear() + self.itemMap.clear() + self._currentIndex = -1 + + def count(self): + """ Returns the number of items in the combobox """ + return len(self.items) + + def insertItem(self, index: int, text: str, icon: Union[str, QIcon, FluentIconBase] = None, userData=None): + """ Inserts item into the combobox at the given index. """ + if not text or text in self.itemMap: + return + + item = ComboItem(text, icon, userData) + self.items.insert(index, item) + self.itemMap[text] = item + + if index <= self.currentIndex(): + self._onItemClicked(self.currentIndex() + 1) + + def insertItems(self, index: int, texts: Iterable[str]): + """ Inserts items into the combobox, starting at the index specified. """ + pos = index + for text in texts: + if not text or text in self.itemMap: + continue + + item = ComboItem(text) + self.items.insert(pos, item) + self.itemMap[text] = item + pos += 1 + + if index <= self.currentIndex(): + self._onItemClicked(self.currentIndex() + pos - index) + def _closeComboMenu(self): if not self.dropMenu: return diff --git a/setup.py b/setup.py index 6a9ebbd2923985ef1f1c874a5f664832bb718a80..39cf8ff9a344c4922af87919a46a4627a9e242b2 100644 --- a/setup.py +++ b/setup.py @@ -6,7 +6,7 @@ with open('README.md', encoding='utf-8') as f: setuptools.setup( name="PyQt-Fluent-Widgets", - version="0.6.5", + version="0.6.6", keywords="pyqt fluent widgets", author="zhiyiYo", author_email="shokokawaii@outlook.com",