[Qt-interest] A new (?) const-correct way to create aUiobjectand do setupUi

Oliver.Knoll at comit.ch Oliver.Knoll at comit.ch
Wed Feb 9 10:40:23 CET 2011


On 2011-02-08 Robert Robert Hairgrove wrote:

>> Anyway, I wouldn't mind if a function like createAndSetupUi would
>> return a non-const reference. ....
> 
> But I thought that the whole motivation for this project was to provide a const UI
> object? 

Well, what ist he motivation of this discussion anyway? ;) Are we talking about a possible future Qt API extension (in the form of some Q_CREATE_UI macro or "setup" method)? Or a design pattern that would be useful for everyone to follow? Or even a design pattern that Qt Creator should apply when auto-generating classes? A question of taste "reference vs pointer"? ;)

For my part I am very happy with the "traditional" ui(new Ui::MyWindow) allocation in the c'tor and a simple "delete ui" in the d'tor, and having a Ui::MyWindow *ui member in the class. Qt Designer creates this code for me, the ui pointer is never dynamically re-allocated during the lifetime of the widget, except if the widget is destroyed. So it is very easy to quickly check the d'tor whether there is really a "delete ui" inside. It is a very simple pattern to use, almost no overhead (if that matters anyway) and hence not very error prone. I think for the time that I would spend thinking about "const reference vs non-const pointer vs const pointer vs..." I have already created 5 widget layouts ;)

Just my 2 cents.

Cheers, Oliver
--
Oliver Knoll
Dipl. Informatik-Ing. ETH
COMIT AG - ++41 79 520 95 22





More information about the Qt-interest-old mailing list