Search code examples
pythonqtsignalspysideqlistview

Qt 4.8.4: Cannot connect slot to QListView::currentChanged() signal


When connecting a slot to QListView::currentChanged(current, previous) signal using auto connection I get:

QMetaObject::connectSlotsByName: No matching signal for on_modelosView_currentChanged(QModelIndex,QModelIndex)

Not using auto connection I get:

AttributeError: 'builtin_function_or_method' object has no attribute 'connect'

I'm using PySide and my code is as follows:

class Modelos(QtGui.QDialog):
def __init__(self, parent):
    QtGui.QDialog.__init__(self, parent)
    self.ui = Ui_Dialog()
    self.ui.setupUi(self)

    # Inicializa o modelo
    self.model = ModelosModel(self)
    self.ui.modelosView.setModel(self.model)
    # Inicializa o mapper
    self.mapper = QtGui.QDataWidgetMapper(self)
    self.mapper.setModel(self.model)
    self.mapper.addMapping(self.ui.modelosEdit, 0)
    self.mapper.toFirst()
    self.ui.modelosView.currentChanged.connect(self.onmodelosView_currentChanged)

@QtCore.Slot(QtCore.QModelIndex, QtCore.QModelIndex)
def onmodelosView_currentChanged(self, current, previous):
    self.mapper.setCurrentIndex(current.row())

Where: ModelosModel is a subclass of QtAbstractListModel and modelosView is a QListView widget.

My goal is to use this signal to update the mapper index so the user can select the item he wants in QListView and edit it in a QPlainTextEdit using a mapper.

Edit: To clear the confusion this is the code that originated the first error:

class Modelos(QtGui.QDialog):
def __init__(self, parent):
    QtGui.QDialog.__init__(self, parent)
    self.ui = Ui_Dialog()
    self.ui.setupUi(self)

    # Inicializa o modelo
    self.model = ModelosModel(self)
    self.ui.modelosView.setModel(self.model)
    # Inicializa o mapper
    self.mapper = QtGui.QDataWidgetMapper(self)
    self.mapper.setModel(self.model)
    self.mapper.addMapping(self.ui.modelosEdit, 0)
    self.mapper.toFirst()

@QtCore.Slot(QtCore.QModelIndex, QtCore.QModelIndex)
def on_modelosView_currentChanged(self, current, previous):
    self.mapper.setCurrentIndex(current.row())

I was clearly using the auto connect feature but I got the error:

QMetaObject::connectSlotsByName: No matching signal for on_modelosView_currentChanged(QModelIndex,QModelIndex)

Solution

  • Ok, I was checking the docs for the tenth time and just realized that QListView::currentChanged(...) is actually a slot and not a signal. I just created a custom subclass of QListView with the signal I needed and made currentChanged emit that signal instead.