0

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)
4

2 に答える 2

0

それはあなたのconnect()声明からではなく、setupUi().

デフォルトでsetupUi()は、 への呼び出しを追加しますQMetaObject::connectSignalsByName(widget)。ここで、widgetはに渡される引数ですsetupUi()(あなたの場合: self)。

self次に、その呼び出しは、名前が似ているのすべてのスロットを探します。

on_ChildObjectName_SignalName

そして、名前が付けられた子オブジェクトがあるかどうかselfChildObjectNameを判断しようとします(の意味でQObject::objectName()。そうであれば、そのスロットに接続しようとしSignalNameます。明らかに、そのようなことはありません。

簡単に言えば、使用する予定がない限り、パターンを使用してスロットに名前を付けないでくださいon_Child_SignalconnectSignalsByName

(一方、Designer を使用して作成されたウィジェットには非常に便利です。Designer は常に子ウィジェットに名前を付けるため、この機能を使用してシグナルに簡単に接続できます。呼び出されたスロットを作成するだけで、on_Child_Signal魔法のように機能します。)

于 2013-02-13T16:22:51.143 に答える
0

OK、ドキュメントを 10 回チェックしていて、QListView::currentChanged(...) が実際にはスロットであり、シグナルではないことに気付きました。必要なシグナルを使用して QListView のカスタム サブクラスを作成し、代わりに currentChanged にそのシグナルを発行させました。

于 2015-07-16T13:29:35.647 に答える