File: qenumcombobox.md

package info (click to toggle)
superqt 0.7.7-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,320 kB
  • sloc: python: 9,108; makefile: 16; sh: 12
file content (72 lines) | stat: -rw-r--r-- 1,713 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# QEnumComboBox

`QEnumComboBox` is a variant of
[`QComboBox`](https://doc.qt.io/qt-6/qcombobox.html) that populates the items in
the combobox based on a python `Enum` class.  In addition to all the methods
provided by `QComboBox`, this subclass adds the methods
`enumClass`/`setEnumClass` to get/set the current `Enum` class represented by
the combobox, and `currentEnum`/`setCurrentEnum` to get/set the current `Enum`
member in the combobox. There is also a new signal `currentEnumChanged(enum)`
analogous to `currentIndexChanged` and `currentTextChanged`.

Method like `insertItem` and `addItem` are blocked and try of its usage will end
with `RuntimeError`

```python
from enum import Enum

from qtpy.QtWidgets import QApplication
from superqt import QEnumComboBox


class SampleEnum(Enum):
    first = 1
    second = 2
    third = 3

app = QApplication([])

combo = QEnumComboBox()
combo.setEnumClass(SampleEnum)
combo.show()

app.exec_()
```

{{ show_widget() }}

Another option is to use optional `enum_class` argument of constructor and change

```python
# option A:
combo = QEnumComboBox()
combo.setEnumClass(SampleEnum)
# option B:
combo = QEnumComboBox(enum_class=SampleEnum)
```

## Allow `None`

`QEnumComboBox` also allows using `Optional` type annotation:

```python
from enum import Enum

from superqt import QEnumComboBox

class SampleEnum(Enum):
    first = 1
    second = 2
    third = 3

# as usual:
# you must create a QApplication before create a widget.

combo = QEnumComboBox()
combo.setEnumClass(SampleEnum, allow_none=True)
```

In this case there is added option `----` and the `currentEnum()` method will
return `None` when it is selected.

{{ show_members('superqt.QEnumComboBox') }}