Skip to content

Commit

Permalink
Add Separator to ComboBox
Browse files Browse the repository at this point in the history
  • Loading branch information
qin-yu committed Mar 14, 2024
1 parent 781a54a commit 8c55825
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/magicgui/backends/_qtpy/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
QTextDocument,
)

from magicgui.types import FileDialogMode
from magicgui.types import FileDialogMode, Separator
from magicgui.widgets import Widget, protocols
from magicgui.widgets._concrete import _LabeledWidget

Expand Down Expand Up @@ -952,9 +952,13 @@ def _mgui_set_choices(self, choices: Iterable[tuple[str, Any]]) -> None:
for i in reversed(range(self._qwidget.count())):
if self._qwidget.itemText(i) not in choice_names:
self._qwidget.removeItem(i)
# update choices
# update choices and insert separators
for name, data in choices_:
self._mgui_set_choice(name, data)
if isinstance(data, Separator):
for _ in range(data.thickness):
self._qwidget.insertSeparator(self._mgui_get_count())
else:
self._mgui_set_choice(name, data)

# if the currently selected item is not in the new set,
# remove it and select the first item in the list
Expand Down
6 changes: 6 additions & 0 deletions src/magicgui/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ def __bool__(self) -> bool:

Undefined = _Undefined()

class Separator:
"""Separator sentinel for ComboBox choices."""
def __init__(self, thickness: int = 1):
self.thickness: int = thickness


JsonStringFormats = Literal[
# ISO 8601 format.
# https://www.iso.org/iso-8601-date-and-time-format.html
Expand Down

0 comments on commit 8c55825

Please sign in to comment.