我可以访问超出范围的对象的 Qt 信号/槽吗?

发布于 2024-10-02 21:35:19 字数 396 浏览 0 评论 0原文

Qt 信号/槽是否遵循本机 C++ 的范围?

假设我有以下类:House、Kitchen、Cellar、StoveShelf

class House   {Kitchen kitchen, Cellar cellar;};
class Kitchen {Stove stove;};
class Cellar  {Shelf shelf;};

现在我想从地窖的架子向厨房的炉子发送信号。唯一的方法是通过将信号从架子连接到地窖、将插槽从厨房连接到炉灶,然后在室内连接地窖和厨房来实现这一点吗?或者有没有办法直接做到这一点?

我有一个需要与用户界面通信的类,我想知道是否需要通过中间类“代理”所有各种信号/槽。或者这是糟糕设计的一个指标?

Do the Qt signals/slots follow the scope of native C++?

Let's say I have the following classes: House, Kitchen, Cellar, Stove and Shelf.

class House   {Kitchen kitchen, Cellar cellar;};
class Kitchen {Stove stove;};
class Cellar  {Shelf shelf;};

Now I want to send a signal from the shelf in the cellar to the stove in the kitchen. Is the only way to do this by connecting a signal from the shelf to the cellar and a slot from kitchen to the stove and then in house connecting cellar and kitchen? Or is there a way to do this directly?

I have a class that needs to communicate with a user interface and I wonder if I need to "proxy" all the various signals/slots through intermediate classes. Or is this an indicator of bad design?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

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

评论(3

等风来 2024-10-09 21:35:19

您可以使用 House 的任何方法进行连接,因为您可以访问这两个对象。 “连接器”必须能够在编译时访问发送者和接收者,仅此而已。

You can do the connection in any method of House, as there you can access both objects. The "connector" must be able to access both sender and receiver, at compile time, that's all there is to it.

自由如风 2024-10-09 21:35:19

您应该能够将 Shelf 实例中的信号链接到

House 中的 Stove 实例,

connect(cellar->shelf,SIGNAL(signalHere()),kitchen->stove,SLOT(slotHere()) );

只需确保 shelfstoveKitchenCellar 中的公共变量,并且您将会被设置

You should be able to just link a signal from the Shelf instance to the Stove instance

in House,

connect(cellar->shelf,SIGNAL(signalHere()),kitchen->stove,SLOT(slotHere()));

just make sure that shelf and stove are public variables in Kitchen and Cellar and you'll be set

腻橙味 2024-10-09 21:35:19

您不能在不是 QObject 的类上使用信号/槽,所以不,您的示例将无法工作。

如果您使用父对象初始化子对象,则可以绕过封装,因此您可以执行肮脏技巧,例如: connect(this->shelf, SIGNAL(signalHere()), kitchen ->children()[0], SLOT(aStoveSlot()))。然而,只有当 Kitchen 的第一个孩子确实是一个 Stove 时,这才有效......因此,由于这是一个明显的依赖项,您应该通过将炉灶公开或通过添加炉灶访问器方法来使其可见。

You cant use signals/slots on classes which are no QObjects, so no, your example wont work whatsoever.

You can circumvent the encapsulation if you initilaize the child objects with their parent object, so you can do dirty tricks like: connect(this->shelf, SIGNAL(signalHere()), kitchen->children()[0], SLOT(aStoveSlot())). however this will only work if the first child of Kitchen is really a Stove... so since this is a obvious dependency, you should make this visible by making stove public, or by adding a stove accessor method.

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