为什么要在 PyQt 中使用槽
我正在研究Qt库。最近我接触到了插槽的概念,到目前为止我将它们理解为作为对某些事件的反应而执行的函数。仅仅是这个还是更多?
为什么他们为如此普通的东西选择这样一个异国情调的名字slots?这个词有很多含义。他们打算考虑 Qt 中的slots
的含义是什么?
谢谢
I'm studying Qt library. Recently I hit the concept of slots
and so far I understand them as functions that are executed as reactions to certain events. Is it only this or something more?
Why did they choose such an exotic name slots for something such ordinary? This word has so many meanings. Which meaning did they intend to thought about slots
in Qt?
thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
术语
signal
和slot
有些不匹配。更好的选择可能是
信号
和接收器
,或者连接器
和插槽
。在C++中,信号只能连接到已定义为槽的函数。尝试将信号连接到普通功能就像尝试将 USB 电缆连接到以太网端口一样。
然而,在 PyQt 中,这种区别并不那么重要,因为任何可调用都可以连接到信号,而不仅仅是预定义的插槽。
话虽如此,最新版本的 PyQt 还提供了 pyqtSlot 装饰器 可用于定义具有多个不同签名的插槽。一般来说,在 PyQt 中,连接到预定义槽(无论是 C++ 还是 PyQt)比连接到普通的 python 可调用更有效。
有关 PyQt 中信号和槽支持的完整详细信息,请参阅参考指南< /a>.
The terms
signal
andslot
are somewhat mismatched.A better choice might have been
signal
andreceiver
, orconnector
andslot
.In C++, a signal can only connect to a function that has been defined as a slot. Trying to connect a signal to an ordinary function would be like trying to connect a usb cable to an ethernet port.
However, in PyQt, this distinction is much less important, because any callable can be connected to a signal, not just predefined slots.
Having said that, more recent versions of PyQt also provide a pyqtSlot decorator which can be used to define a slot with several different signatures. And in general, in PyQt it is more efficient to connect to a predefined slot (whether C++ or PyQt) than to an ordinary python callable.
For full details of the signal and slot support in PyQt, see the reference guide.
插槽与信号一起出现。您将信号连接到插槽。这是Qt本身的核心概念,并不是PyQt特有的。
Qt 文档有详细信息: Signals &插槽。
这是一个非常方便的概念,可以使代码中的层分离得相当干净。
插槽本身是普通函数(在 C++ 中),您可以将它们用作普通函数以及“信号接收器”。
Slots go with signals. You connect signals to slots. This is a core concept in Qt itself, it is not specific to PyQt.
The Qt documentation has the details: Signals & Slots.
It's just a very handy concept, makes for rather clean separation of layers in your code.
The slots themselves are plain functions (in C++), you can use them as normal functions as well as as "signal receivers".