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.
Qt 4.8.4: Cannot connect slot to QListView::currentChanged() signal
Question
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)
La solution 2
Autres conseils
It's not coming from your connect()
statement, but from setupUi()
.
By default, setupUi()
adds a call to QMetaObject::connectSignalsByName(widget)
, where widget
is the argument passed to setupUi()
(in your case: self
).
That call, in turn, will look for all the slots of self
with a name resembling
on_ChildObjectName_SignalName
and will try to figure out if self
has a child object named ChildObjectName
(in the sense of QObject::objectName()
; if so, it will try to connect its SignalName
to that slot. Obviously you don't anything like that.
Long story short: don't name your slots using the on_Child_Signal
pattern unless you plan to use connectSignalsByName
.
(On the other hand, it's quite convenient for widgets created using Designer: since Designer always give child widgets a name, you can easily hook up to their signals by using this feature, just create a slot called on_Child_Signal
and it will magically work.)