为什么要在 PyQt 中使用槽

发布于 2024-12-11 08:36:02 字数 240 浏览 0 评论 0原文

我正在研究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技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

临走之时 2024-12-18 08:36:02

术语signalslot有些不匹配。

更好的选择可能是信号接收器,或者连接器插槽

在C++中,信号只能连接到已定义为槽的函数。尝试将信号连接到普通功能就像尝试将 USB 电缆连接到以太网端口一样。

然而,在 PyQt 中,这种区别并不那么重要,因为任何可调用都可以连接到信号,而不仅仅是预定义的插槽。

话虽如此,最新版本的 PyQt 还提供了 pyqtSlot 装饰器 可用于定义具有多个不同签名的插槽。一般来说,在 PyQt 中,连接到预定义槽(无论是 C++ 还是 PyQt)比连接到普通的 python 可调用更有效。

有关 PyQt 中信号和槽支持的完整详细信息,请参阅参考指南< /a>.

The terms signal and slot are somewhat mismatched.

A better choice might have been signal and receiver, or connector and slot.

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.

东走西顾 2024-12-18 08:36:02

插槽与信号一起出现。您将信号连接到插槽。这是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".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文