Using QSliders with model/view programming in PyQt
-
wrote on 14 Oct 2013, 02:26 last edited by
I'm working on an application where I have a bunch of QSliders that I'd like to have update a model when they are changed. Similarly, I'd like the values of the sliders to update whenever the model is changed. I have been attempting to use the QDataWidgetMapper and QItemDelegate classes to accomplish this, but without much success.
Here is my model class:
@class dataModel(QtCore.QAbstractListModel):
def __init__(self, initData, parent=None): super(dataModel, self).__init__(parent) self.__data = initData def data(self, index, role=QtCore.Qt.DisplayRole): if not index.isValid(): return None if index.row() > len(self.__data): return None if role == QtCore.Qt.DisplayRole or role == QtCore.Qt.EditRole: return self.__data[index.row()] return None def rowCount(self, parent=QtCore.QModelIndex()): return len(self.__data) def setData(self, index, value, role=QtCore.Qt.EditRole): if not index.isValid() or role != QtCore.Qt.EditRole: return False self.__data[index.row()] = value self.dataChanged.emit(index, index) return True@
Here is the delegate class:
@class sliderDelegate(QtGui.QItemDelegate):
'''
classdocs
'''
def init(self, parent=None):
'''
Constructor
'''
super(sliderDelegate, self).init(parent)def setEditorData(self, editor, index): editor.setValue(index.model().data(index, QtCore.Qt.EditRole)) def setModelData(self, editor, model, index): model.setData(index, editor.value(), QtCore.Qt.EditRole)@
And here is the setup code:
@ self._model = dataModel([])
self._parameterMapper = QtGui.QDataWidgetMapper(mainWindowInstance)
self._parameterMapper.setModel(self._model)
self._parameterMapper.setItemDelegate(sliderDelegate(mainWindowInstance))
self._parameterMapper.addMapping(self._mainWindowInstance.ui.mySlider, 0)
self._parameterMapper.toFirst()@Unfortuantely, I get the following error when toFirst is called:
@ editor.setValue(index.model().data(index, QtCore.Qt.EditRole))
AttributeError: 'NoneType' object has no attribute 'data'@How can I get this to work correctly? Thanks for any help.
1/1