为什么设计者生成的 UI 类嵌入为“聚合”?无法实现自定义插槽?
我看到设计器生成的 UI 类可以使用 Qt 中的以下任何方法嵌入,
- 聚合作为指针成员
- 聚合
- 多个,私有继承
但是据说第二种方法不支持自定义槽位。有人可以详细说明一下吗?为什么我们不能在使用聚合的同时实现自定义槽?
另外,详细说明每种方法的优点和缺点。
I see that the designer generated UI classes be embedded using any of the following methods in Qt,
- Aggregation as a pointer member
- Aggregation
- Multiple, Private inheritance
but it is said that the second method doesn't support custom slots. Can someone elaborate on this? Why can't we implement custom slots, while using aggregation?
Also, elaborate on the advantages and disadvantages in each of the methods.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不认为第二个选项不支持自定义插槽。
Qt 官方文档中讨论了该选择。请参阅 http:// /doc.qt.io/qt-4.8/designer-using-a-ui-file.html#compile-time-form-processing
但是,请注意,本文档中介绍的三种方法并不对应于Qt Creator 中提供了三个选项。文档中介绍的第一种方法“直接方法”不是此处的三个选择之一 - 此方法无法通过此设置控制的 Qt Creator 功能来使用。第二个选项(聚合或“单继承方法”)有两种形式,略有不同的是 ui 类成员是作为数据成员(第二个选项)还是作为指针成员(第一个选项)。
我更喜欢第三种选择,多重继承。这也是使用 Qt 4 进行 C++ GUI 编程(第一版)中使用的方法可免费在线获取),这称这种方法是最干净的。当我编写类时,我并没有真正考虑两个对象,一个是 UI,另一个是功能,我只考虑其中一个,而多重继承是最好的匹配。但文档给出了为什么“聚合作为指针成员”是默认值的原因。
I don't think it's true that the second option doesn't support custom slots.
The choice is discussed in the official Qt documentation. See http://doc.qt.io/qt-4.8/designer-using-a-ui-file.html#compile-time-form-processing
However, note that the three approaches presented in this document do not correspond to the three options presented in Qt Creator. The first approach presented in the document, The Direct Approach, is not one of the three choices here -- this approach is not available through the Qt Creator feature which this setting controls. The second option (aggregation, or "the single inheritance approach") is available in two varieties, the slight variation being whether the ui class member is as a data member (the second option) or as a pointer member (the first option).
My preference is the third option, multiple inheritance. This is also the way used throughout C++ GUI Programming with Qt 4 (first edition available for free online), which calls this approach the cleanest. When I'm writing my class, I'm not really thinking in terms of two objects, one with the UI and the rest with the functionality, I'm thinking about just one, and multiple inheritance matches that the best. But the document gives the reasons why "aggregation as a pointer member" is the default.
QT5 更新:
现在您可以以更多方式支持自定义插槽,例如使用 lambda 和插槽的普通函数支持 (请在此处查看)。
另请注意,推荐的选项是 指针成员,也称为 Pimpl 惯用法。优点是 UI 对象的前向声明将允许更大的项目更快的编译时间,并且共享库将很容易打包 (如此处所述)。
A QT5 Update:
Nowadays you can support custom slots in more fashions like using the lambda and normal function support for slots (check here).
Also, notice that the recommended option is the Pointer Member, also known as the Pimpl idiom. The advantage is the forward declaration of the UI object will allow faster compilation times for bigger projects and also shared libraries will be easily to package (as stated here).