提交 40415468 编写于 作者: 之一Yo's avatar 之一Yo

添加更多下拉框 api

上级 80551f82
...@@ -12,7 +12,7 @@ Examples are available at https://github.com/zhiyiYo/PyQt-Fluent-Widgets/tree/ma ...@@ -12,7 +12,7 @@ Examples are available at https://github.com/zhiyiYo/PyQt-Fluent-Widgets/tree/ma
:license: GPLv3, see LICENSE for more details. :license: GPLv3, see LICENSE for more details.
""" """
__version__ = "0.6.5" __version__ = "0.6.6"
from .components import * from .components import *
from .common import * from .common import *
......
...@@ -31,10 +31,7 @@ class ComboItem: ...@@ -31,10 +31,7 @@ class ComboItem:
""" """
self.text = text self.text = text
self.userData = userData self.userData = userData
if icon: self.icon = icon
self._icon = QIcon(icon) if isinstance(icon, str) else icon
else:
self._icon = QIcon()
@property @property
def icon(self): def icon(self):
...@@ -43,6 +40,13 @@ class ComboItem: ...@@ -43,6 +40,13 @@ class ComboItem:
return self._icon.icon() 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): class ComboBoxBase(QObject):
""" Combo box base """ """ Combo box base """
...@@ -75,7 +79,7 @@ class ComboBoxBase(QObject): ...@@ -75,7 +79,7 @@ class ComboBoxBase(QObject):
return super().eventFilter(obj, e) 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 """ add item
Parameters Parameters
...@@ -127,7 +131,7 @@ class ComboBoxBase(QObject): ...@@ -127,7 +131,7 @@ class ComboBoxBase(QObject):
def currentIndex(self): def currentIndex(self):
return self._currentIndex return self._currentIndex
def setCurrentIndex(self, index): def setCurrentIndex(self, index: int):
""" set current index """ set current index
Parameters Parameters
...@@ -151,6 +155,12 @@ class ComboBoxBase(QObject): ...@@ -151,6 +155,12 @@ class ComboBoxBase(QObject):
return self.items[self.currentIndex()].text 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): def setCurrentText(self, text):
""" set the current text displayed in combo box, """ set the current text displayed in combo box,
text should be in the item list text should be in the item list
...@@ -186,17 +196,36 @@ class ComboBoxBase(QObject): ...@@ -186,17 +196,36 @@ class ComboBoxBase(QObject):
self.setText(text) self.setText(text)
def itemData(self, index: int): 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): if not 0 <= index < len(self.items):
return None return None
return self.items[index].userData 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): 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): if 0 <= index < len(self.items):
self.items[index].userData = value 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): def findData(self, data):
""" Returns the index of the item containing the given data, otherwise returns -1 """ """ Returns the index of the item containing the given data, otherwise returns -1 """
for i, item in enumerate(self.items): for i, item in enumerate(self.items):
...@@ -212,6 +241,43 @@ class ComboBoxBase(QObject): ...@@ -212,6 +241,43 @@ class ComboBoxBase(QObject):
return self.items.index(self.itemMap[text]) 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): def _closeComboMenu(self):
if not self.dropMenu: if not self.dropMenu:
return return
......
...@@ -6,7 +6,7 @@ with open('README.md', encoding='utf-8') as f: ...@@ -6,7 +6,7 @@ with open('README.md', encoding='utf-8') as f:
setuptools.setup( setuptools.setup(
name="PyQt-Fluent-Widgets", name="PyQt-Fluent-Widgets",
version="0.6.5", version="0.6.6",
keywords="pyqt fluent widgets", keywords="pyqt fluent widgets",
author="zhiyiYo", author="zhiyiYo",
author_email="shokokawaii@outlook.com", author_email="shokokawaii@outlook.com",
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册