[04f775d] | 1 | from PyQt5 import QtGui, QtWidgets |
---|
| 2 | |
---|
| 3 | |
---|
| 4 | class AssociatedComboBox(QtWidgets.QComboBox): |
---|
| 5 | """Just a regular combo box, but associated with a particular QStandardItem so that it can be used to display and |
---|
| 6 | select an item's current text and a restricted list of other possible text. |
---|
| 7 | |
---|
| 8 | When the combo box's current text is changed, the change is immediately reflected in the associated item; either |
---|
| 9 | the text itself is set as the item's data, or the current index is used; see constructor.""" |
---|
| 10 | item = None # type: QtGui.QStandardItem |
---|
| 11 | |
---|
| 12 | def __init__(self, item, idx_as_value=False, parent=None): |
---|
| 13 | # type: (QtGui.QStandardItem, bool, QtWidgets.QWidget) -> None |
---|
| 14 | """ |
---|
| 15 | Initialize widget. idx_as_value indicates whether to use the combo box's current index (otherwise, current text) |
---|
| 16 | as the associated item's new data. |
---|
| 17 | """ |
---|
| 18 | super(AssociatedComboBox, self).__init__(parent) |
---|
| 19 | self.item = item |
---|
| 20 | |
---|
| 21 | if idx_as_value: |
---|
| 22 | self.currentIndexChanged[int].connect(self._onIndexChanged) |
---|
| 23 | else: |
---|
| 24 | self.currentTextChanged.connect(self._onTextChanged) |
---|
| 25 | |
---|
| 26 | def _onTextChanged(self, text): |
---|
| 27 | # type: (str) -> None |
---|
| 28 | """ |
---|
| 29 | Callback for combo box's currentTextChanged. Set associated item's data to the new text. |
---|
| 30 | """ |
---|
| 31 | self.item.setText(text) |
---|
| 32 | |
---|
| 33 | def _onIndexChanged(self, idx): |
---|
| 34 | # type: (int) -> None |
---|
| 35 | """ |
---|
| 36 | Callback for combo box's currentIndexChanged. Set associated item's data to the new index. |
---|
| 37 | """ |
---|
| 38 | self.item.setText(str(idx)) |
---|