问题描述
在 Qt Designer 中,我创建了一个 QDialog
窗口并使用 pysideuic
将其编译为包含一个 setupUi
方法初始化所有GUI 元素以及我扩展以实现功能的元素,如下所示:
In Qt Designer, I created a QDialog
window and used pysideuic
to compile that to a base class which contains a setupUi
method initialising all GUI elements and which I extend to implement the functionality, as so:
class MyDialog(QtGui.QDialog, ui_file.Ui_main_dialog):
def __init__(self, parent=None):
QtGui.QDialog.__init__(self, parent)
ui_file.Ui_main_dialog.__init__(self)
self.setupUi(self)
这个 setupUi
方法调用了 QtCore.QObject.connect
以获取我在 Qt Designer 中创建的信号槽连接,我还在其中向 GUI 添加了新槽.这些插槽在 pysideuic
生成的基类中不存在,我将它们添加到 MyDialog
类中,例如
This setupUi
method has calls to QtCore.QObject.connect
for the signal-slot connections I created in Qt Designer, where I also added new slots to the GUI. These slots are non-existent in the base class generated by pysideuic
and I added them to the MyDialog
class, e.g.
def on_list_selection_changed(self):
self.run_btn.setEnabled(len(self.modules_list.selectedIndexes()) > 0)
在这个例子中,槽在 Qt Designer 中被称为 on_list_selection_changed()
(参数列表为空).
For this example, the slot is called on_list_selection_changed()
(with empty parameter list) in Qt Designer.
在初始化时,MyDialog.__init__
调用 Ui_main_dialog.setupUi
,后者最终调用 QtCore.QMetaObject.connectSlotsByName
(后两个带有 MyDialog
实例的 self
当前正在创建).这会在 sys.stderr
上发出以下行:
On initialisation, MyDialog.__init__
calls Ui_main_dialog.setupUi
, which eventually calls QtCore.QMetaObject.connectSlotsByName
(the latter two with the MyDialog
instance's self
which is currently being created). This emits the following line on sys.stderr
:
QMetaObject::connectSlotsByName: No matching signal for on_list_selection_changed()
尽管如此,当连接的modules_list.itemSelectionChanged()
(modules_list
是一个QListWidget
)时,信号表现正确并且槽被调用.
Still, the signal behaves correctly and the slot is called when the connected modules_list.itemSelectionChanged()
(modules_list
is a QListWidget
).
所以这是我的问题:为什么我会收到此警告?鉴于它似乎无关紧要,我该怎么做才能让它不出现?
So here is my question: why do I receive this warning? What can I do so it doesn't appear, given that it seems to be irrelevant?
编辑:由于我在过去 5 个月内没有收到任何答案,所以我想我给出了一个完整的示例,以便更容易重现问题.
Edit: Since I didn't receive any answers in the last 5 months, I thought I give a complete example to make reproducing the problem easier.
这个例子与上面的问题不同,它只使用了一个 QLineEdit
实例.代码如下:
This example differs from the question above in that it only uses a QLineEdit
instance. Here is the code:
import sys
from PySide import QtCore, QtGui
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
self.lineEdit = QtGui.QLineEdit(Dialog)
self.lineEdit.setObjectName("lineEdit")
QtCore.QObject.connect(self.lineEdit, QtCore.SIGNAL("textChanged(QString)"), Dialog.on_lineEdit_changed)
QtCore.QMetaObject.connectSlotsByName(Dialog)
class MainWindow(QtGui.QMainWindow, Ui_Dialog):
def __init__(self, parent=None):
QtGui.QMainWindow.__init__(self, parent)
Ui_Dialog.__init__(self)
Ui_Dialog.setupUi(self, self)
@QtCore.Slot(unicode) # signal with no arguments
def on_lineEdit_changed(self, text):
print 'Changed to', text
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
mw = MainWindow()
mw.show()
sys.exit(app.exec_())
请注意,Ui_Dialog
类的代码是由 Qt Designer 的 .ui 文件中的 pysideuic
生成的,但我将其缩短了一点以更好地突出问题.
Note that the code for the Ui_Dialog
class is generated by the pysideuic
from the Qt Designer's .ui file, but I shortened it a bit to better highlight the problem.
推荐答案
我在 C++ 中遇到了同样的问题.connectSlotsByName 是硬编码的,如果它发现无法自动连接的插槽,则输出该消息,即使您不需要自动连接,因为您已经明确地自己完成了.您可以使用 qInstallMsgHandler 通常抑制警告或将消息写在更好的地方,但我认为没有任何方法可以告诉 connectSlotsByName 您不特别关心这种情况.
I have the same issue in C++. connectSlotsByName is hard-coded to output that message if it finds a slot it can't connect automatically, even though you don't need the automatic connection because you've done it explicitly yourself. You can use qInstallMsgHandler to suppress warnings in general or write the messages somewhere better but I don't think there's any way to tell connectSlotsByName that you don't care about this case in particular.
这篇关于PySide:QMetaObject.connectSlotsByName 发出警告“没有匹配的信号...";但仍然有效..?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!