Why is that the designer generated UI classes embedded as "Aggregation" can't implement custom slots?
I see that the designer generated UI classes be embedded using any of开发者_StackOverflow社区 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.
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.
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).
精彩评论