I'm trying to use a QDataWidgetMapper
to drive a custom QComboBox
using Enum
.
I'm on Windows 10, using Python 2.7.13, PySide 1.2.4 (Qt 4.8.7), here's the code:
from PySide.QtCore import Qt, Property
from PySide.QtGui import (
QApplication,
QComboBox,
QDataWidgetMapper,
QFormLayout,
QLineEdit,
QMainWindow,
QPushButton,
QStandardItem,
QStandardItemModel,
QWidget,
)
from enum import Enum
class State(Enum):
foo = 1
bar = 2
baz = 3
class StateEdit(QComboBox):
def __init__(self, parent=None):
super(StateEdit, self).__init__(parent)
self.addItems(State._member_names_)
def state(self):
text = self.currentText()
return State[text] if text else None
def setState(self, value):
if value is None:
index = -1
else:
index = self.findText(value.name, Qt.MatchExactly)
self.setCurrentIndex(index)
state = Property(State, state, setState, user=True)
class Window(QMainWindow):
def __init__(self, parent=None):
super(Window, self).__init__(parent)
self._name_edit = QLineEdit()
self._state_edit = StateEdit()
self._state_button = QPushButton('Change State')
self._content = QWidget()
self.setCentralWidget(self._content)
self._form = QFormLayout()
self._content.setLayout(self._form)
self._form.addRow('Name', self._name_edit)
self._form.addRow('State', self._state_edit)
self._form.addRow('Action', self._state_button)
self._state_button.released.connect(self._on_state_button_clicked)
self._model = QStandardItemModel()
name_item = QStandardItem()
state_item = QStandardItem()
name_item.setText('My Name')
state_item.setData(State.bar)
self._model.appendRow([name_item, state_item])
self._mapper = QDataWidgetMapper()
self._mapper.setModel(self._model)
self._mapper.addMapping(self._name_edit, 0)
self._mapper.addMapping(self._state_edit, 1, 'state')
self._mapper.toFirst()
def _on_state_button_clicked(self):
self._state_edit.state = State.baz
def data(self):
return {
'name': self._name_edit.text(),
'state': self._state_edit.state,
}
if __name__ == "__main__":
import sys
from pprint import pprint
app = QApplication(sys.argv)
win = Window()
win.show()
app.exec_()
pprint(win.data())
The problem is, the enum widget always pops up displaying its first index.
There seems to be no issue with the property itself, as setting it using the button works.
The state is also updated when choosing a different index using the combo box, as demonstrated by the data printed when the window closes.
I've looked into properties and dynamic properties, the user flag on properties, even overriding setProperty
and property
on the widget, to no avail.
I've also looked into this guide, but it seems regular issues with QComboBox
and QDataWidgetMapper
don't really apply to my case.
The only solution I see is to use the regular workflow with QComboBox
and just use plain old indices instead of enum values, but this would be a shame, I just need the initial mapping to be properly triggered and everything would work perfectly.
I don't really know where to look anymore, maybe it's a PySide specific issue, so any pointers will help !