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

Robert Hairgrove evorgriahr at hispeed.ch
Wed Feb 9 12:37:25 CET 2011


On Wed, 2011-02-09 at 08:13 +0100, Niels Dekker (Qt-interest) wrote:
> FWIW, I think it's also more convenient to write e.g., 
> "ui.pushButton" than "ui->pushButton".

If you use QtCreator to edit code, you'll see that typing "." after a
pointer name gives you automagically "->" (besides giving you a
drop-down list of all the class members).

> > besides, I have yet to see the real benefit of having the
> > UI member const. Who should change it? Either it is a private
> > subclass or a private member variable (pointer or not).
> 
> I'm following Scott Meyers' recommendation to use const whenever 
> possible (Effective C++ Third Edition, item 3). Even for private data. 
> But of course, you are free to do otherwise.

It is item 21 in my copy of the second edition. :)

> However, I think a function like CreateAndSetupUi should not stop people 
> from doing retranslateUi at any time (if necessary). Do I understand 
> correctly that the call retranslateUi might as well be applied to a 
> *copy* of the ui, while still having the same effect? For a ui of type 
> UiClass, isn't ui.retranslateUi(this) equivalent to 
> UiClass(ui).retranslateUi(this)?

Yes, but it seems rather inefficient (and illogical) to me to have to
create a copy of an object just to change some strings (although a
shallow copy of a Ui would do the job, so it really wouldn't be that
inefficient).

I don't see any real advantages to changing the const semantics of the
ui object, though. If it is a private member (or subclass) of the owner
class, then it is inaccessible outside that class. No other class can
change it. And making it "const" sends the wrong message, IMHO ... we
can change all the UI elements through their pointers anyway, so the Ui
really isn't "const" in that sense, i.e.: "Don't change my Ui!".

And unless setupUi() and retranslateUi() are made const functions, it
means that these cannot be called if the Ui object is a const reference.
I see no need to make that change (although it really wouldn't make any
difference when creating and using the Ui in the customary fashion).




More information about the Qt-interest-old mailing list