我一直在遵循最初用PyQt4编写的一些代码。我正在使用PyQt5,想了解如何更改SIGNAL代码才能正常工作。
我知道新方法遵循这种格式,但是我似乎不太了解。
sender.signal.connect(slot)
PyQt4代码:
# QtCore.QObject.connect(self.color_preferences_window, QtCore.SIGNAL(
# "return_color_prefs()"), self.finished_changing_colors)
# QtCore.QObject.connect(self.value_preferences_window, QtCore.SIGNAL(
# "return_value_prefs()"), self.finished_changing_values)
# QtCore.QObject.connect(self.grid, QtCore.SIGNAL(
# "return_current_cell_attributes(PyQt_PyObject)"), self.update_current_cell_info)
我的尝试:(失败)
self.color_preferences_window.return_color_prefs.connect(self.finished_changing_colors)
self.value_preferences_window.return_value_prefs.connect(self.finished_changing_values)
self.grid.return_current_cell_attributes(self.update_current_cell_info)
编辑:
这里定义函数的方式:
def finished_changing_values(self):
# called by the value preferences window when the user is done
if self.value_preferences_window.is_valid:
self.value_preferences_window.hide_window()
new_value_prefs = self.value_preferences_window.values
new_value_attribs = self.value_preferences_window.attribs
for attrib, value in list(zip(new_value_attribs, new_value_prefs)):
self.grid.set_attrib_value(attrib, value)
new_line_types = self.value_preferences_window.current_line_types
line_names = self.value_preferences_window.lines
for attrib, value in list(zip(line_names, new_line_types)):
self.grid.set_line_type(attrib, value)
self.grid.repaint()
self.setEnabled(True)
self.setWindowTitle(
"AI Project 1 - (Width:"+str(self.size().width())+", Height:"+str(self.size().height())+")")
谢谢!
from PyQt5.QtCore import QObject, pyqtSignal
class Foo(QObject):
range_changed = pyqtSignal(int, int) # args signature has to match with slot
class OtherClass(QObject):
def __init__(self, first_class_ref, parent=None):
super().__init__(parent)
first_class_ref.range_changed.connect(self.my_slot)
def my_slot(self, arg1, arg2):
print(f"Slot triggered with: {arg1}, {arg2}")
# use it more-less like that:
one = Foo()
other = OtherClass(one)