Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Using QSliders with model/view programming in PyQt

Using QSliders with model/view programming in PyQt

Scheduled Pinned Locked Moved General and Desktop
1 Posts 1 Posters 765 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    bitrex
    wrote on 14 Oct 2013, 02:26 last edited by
    #1

    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 Reply Last reply
    0

    1/1

    14 Oct 2013, 02:26

    • Login

    • Login or register to search.
    1 out of 1
    • First post
      1/1
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved