python - PyQt6教程-如何接收信号参数

标签 python pyqt6

我是 Python Qt 编程的新手。我一直在通过链接阅读教程 - https://www.pythonguis.com/tutorials/pyqt6-signals-slots-events/

我无法理解的教程部分在“接收数据”部分下

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        self.setWindowTitle("My App")

        button = QPushButton("Press Me!")
        button.setCheckable(True)
        button.clicked.connect(self.the_button_was_clicked)
        button.clicked.connect(self.the_button_was_toggled)

        self.setCentralWidget(button)

    def the_button_was_clicked(self):
        print("Clicked!")

    def the_button_was_toggled(self, checked):
        print("Checked?", checked)

问题

  1. 作者如何能够将参数“checked”传递给函数“the_button_was_toggled”,因为在连接信号“clicked”时我们没有为该函数指定任何参数。对我来说,这似乎更像是一件神奇的事情,而不是我可以通过阅读有关从信号接收参数到插槽的相关文档来理解的东西
  2. 有人可以提供 PyQt6 文档或教程的任何相关链接以更好地理解这一点吗

谢谢你的时间

最佳答案

为了具体回答这种情况,连接到该方法的信号是 QPushButton.clicked 信号。

有点困惑,如果你去Qt的QPushButton documentation您不会发现任何提及此信号的信息,因为它是从 QAbstractButton 继承的。如果您查看该文档页面的顶部,您会看到“Inherits: QAbstractButton”以及指向 the QAbstractButton documentation 的链接。 .

在该页面上,您可以找到 .clicked 信号定义。

void QAbstractButton::clicked(bool checked = false) This signal is emitted when the button is activated (i.e., pressed down then > released while the mouse cursor is inside the button), when the shortcut key > is typed, or when click() or animateClick() is called.

您可以在该定义中看到 clicked 信号在 emitted 时传递了 checked 参数。该值来自按钮本身,用于通知连接的插槽按钮的检查状态。

该状态只有在按钮是可检查的情况下才会改变(因此通常它总是为 false)。

关于python - PyQt6教程-如何接收信号参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72213191/

相关文章:

python - 从 SharedCtypes 数组设置 Tkinter 标签文本

python - 将 Python "Mixer"库与带有构造函数参数的 SQL Alchemy 模型一起使用

python - 如何扩展 barh 中 y 刻度的空间 - python matplotlib

python - 《如何像计算机科学家一样思考》中的 While 循环

python - 如何从循环中创建的组合框获取反馈

python - QMouseEvent'对象没有属性 'pos'

python - 比较两个列表并在python中获取一个新列表

python - 如何使用 webEngineContextLog 禁用 QWebEngineView 日志记录?

python - 在 PyQt6 中尝试使用信号终止线程中的函数失败