公开所包含小部件的信号/槽的正确 Qt 习惯用法是什么?

发布于 2024-08-27 07:17:17 字数 510 浏览 7 评论 0原文

假设我有一个包含 MySubWidgetMyWidget,例如包含文本字段或其他内容的自定义小部件。我希望其他类能够连接到包含的 MySubWidget 实例公开的信号和槽。传统的方法是:

  1. 通过 MyWidget 中的 subWidget() 方法公开指向 MySubWidget 实例的指针
  2. ,复制MyWidget 类中的 MySubWidget 并编写“转发”代码
  3. 还有其他吗?

选择 1 似乎是最少的代码,但它也有点破坏封装,因为现在其他类知道 MyWidget 包含的小部件是什么,并且可能会依赖于它们的功能。

选择 2 似乎保留了封装,但它有很多看似冗余且可能复杂的代码,破坏了整个信号和槽系统的优雅。

在这种情况下通常会做什么?

Suppose I have a MyWidget which contains a MySubWidget, e.g. a custom widget that contains a text field or something. I want other classes to be able to connect to signals and slots exposed by the contained MySubWidget instance. Is the conventional way to do this:

  1. Expose a pointer to the MySubWidget instance through a subWidget() method in MyWidget
  2. Duplicate the signals and slots of MySubWidget in the MyWidget class and write "forwarding" code
  3. Something else?

Choice 1 seems like the least code, but it also sort of breaks encapsulation, since now other classes know what the contained widgets of MyWidget are and might become dependent on their functionality.

Choice 2 seems like it keeps encapsulation, but it's a lot of seemingly redundant and potentially convoluted code that kind of messes up the elegance of the whole signals and slots system.

What is normally done in this situation?

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

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

发布评论

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

评论(3

断念 2024-09-03 07:17:17

如果您查看 Qt 自己的代码,他们更喜欢选项 2。

例如,查看 QTabWidgetQTabBar。它们共享许多信号和槽,但 QTabWidget 隐藏了它使用 QTabBar 的事实(好吧,有点...... QTabWidget::tabBar() 显然打破了这一点,即使它是受保护的)。

虽然这会导致更多的代码,但我认为对于封装来说这是值得的。

不要忘记,您可以将信号连接到信号,如下所示:

connect(mySubWidget, SIGNAL(internalSignal(int)), this, SIGNAL(externalSignal(int)));

MySubWidget 发出 时,这将使 MyWidget 发出 externalSignal(int)内部信号(int)。这至少对信号方面有帮助。不幸的是,我不知道有什么简单的方法可以对老虎机执行相同的操作。

If you look at Qt's own code they prefer option 2.

For example, look at QTabWidget and QTabBar. They share a number of signals and slots, yet QTabWidget hides the fact that it uses a QTabBar (well, sorta... QTabWidget::tabBar() obviously breaks this even though it's protected).

Although this will result in more code, I think it's worth it for the encapsulation.

Don't forget that you can connect signals to signals like so:

connect(mySubWidget, SIGNAL(internalSignal(int)), this, SIGNAL(externalSignal(int)));

Which will make MyWidget emit externalSignal(int) when MySubWidget emits internalSignal(int). This helps with the signal side of things at least. I don't know of any easy way to do the same for slots, unfortunately.

沫尐诺 2024-09-03 07:17:17

我不担心信号和槽的封装,大概只有一件事是进行连接,那么了解一些子类有什么坏处呢?如果您在一个更大的团队中工作,那么防止滥用 subWidget() 方法是一个问题,但您可以使用friend关键字或其他关键字来做到这一点......

I wouldn't worry about encapsulation of signals and slots, presumably only one thing is doing the connecting, so what's the harm in it knowing about some subclasses? If you are working on a larger team preventing abuse of the subWidget() method is a concern but you could do that with the friend keyword or whatever...

不念旧人 2024-09-03 07:17:17

您可以公开包含较少功能的 MySubWidget 基类。

You could expose a base class of MySubWidget that contains less functionality.

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