简体   繁体   English

使用 QtPushButton.clicked 信号传递检查状态

[英]Passing check state with QtPushButton.clicked signal

I'm working on a project and trying to pass the checked state of a checkable button to a function via button.clicked.connect(self.button_checked) and ran across an issue.我正在开发一个项目,并尝试通过button.clicked.connect(self.button_checked)将可检查按钮的选中状态传递给函数并遇到问题。 If I use the "decoration" @QtCore.pyqtSlot() before my callable then the checked state will not pass.如果我在可调用之前使用“装饰” @QtCore.pyqtSlot() ,那么检查状态将不会通过。 But if I remove the decoration the function works as expected and written in documentation.但是,如果我移除装饰,该功能将按预期工作并在文档中编写。 See code below for the two cases:两种情况见下面的代码:

With decoration带装饰

...
btn = QPushButton("Push")
btn.setCheckable(True)
btn.toggled.connect(self.button_checked)
...
@QtCore.pyqtSlot()
def button_checked(self, checked):
    print("checked", checked)
...

I get the error TypeError: button_checked() missing 1 required positional argument: 'checked' when the button is pushed.我收到错误TypeError: button_checked() missing 1 required positional argument: 'checked'按下按钮时。 But when I remove QtCore.pyqtSlot() and just have:但是当我删除QtCore.pyqtSlot()并且只有:

...
btn = QPushButton("Push")
btn.setCheckable(True)
btn.toggled.connect(self.button_checked)
...
def button_checked(self, checked):
    print("checked", checked)
...

The button works as expected and prints checked True or checked False depending on the checked state.该按钮按预期工作,并根据选中状态打印checked Truechecked False

What is the @QtCore.pyqtSlot() for? @QtCore.pyqtSlot()有什么用? I am still new to QtPy and don't have a great grasp on this lines purpose.我还是 QtPy 的新手,对这条线的目的没有很好的把握。 Do I need to explicitly assign a slot in this case?在这种情况下我需要明确分配一个插槽吗? I've found that it can be necessary in some specific situations ( function of pyqtSlot ) and is considered good practice, but it seems to have an effect I don't want in this case.我发现在某些特定情况下( pyqtSlot 的功能)可能需要这样做,并且被认为是好的做法,但在这种情况下它似乎有我不想要的效果。

I get the error TypeError: button_checked() missing 1 required positional argument: 'checked'我收到错误 TypeError: button_checked() missing 1 required positional argument: 'checked'

That's because your slot doesn't take any arguments.那是因为您的插槽不接受任何参数。 Change @QtCore.pyqtSlot() to @QtCore.pyqtSlot(bool)@QtCore.pyqtSlot()更改为@QtCore.pyqtSlot(bool)

My question is what is the @QtCore.pyqtSlot() for?我的问题是 @QtCore.pyqtSlot() 有什么用?

It is used by the QMetaObjectSystem that is required by C++ Qt to do runtime dynamic stuff, like calling slots with the right signature.它被 C++ Qt 所需的QMetaObjectSystem用于执行运行时动态操作,例如调用具有正确签名的槽。 Read more here .在这里阅读更多 In the Python case it is not needed due to the dynamic nature of Python.在 Python 的情况下,由于 Python 的动态特性,不需要它。

Nevertheless it is required if you want to expose your slots away from Python, ie, to QML , and it is said to be slightly faster .然而,如果您想将您的插槽从 Python (即QML )暴露出来,它是必需的,据说它会稍微快一些

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM