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

Niels Dekker (Qt-interest) mymailfromqt at xs4all.nl
Wed Feb 9 14:10:42 CET 2011


>> FWIW, I think it's also more convenient to write e.g.,
>> "ui.pushButton" than "ui->pushButton".

Robert Hairgrove wrote:
> 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).

That's a cool feature. But anyway, I'd rather avoid "->" if "." will do. 
When doing a code review, every "->" triggers the question whether the left 
hand side *might* be NULL. Of course, for "ui->pushButton", the answer is 
usually no. But it distracts my eyes from the more risky "->" cases.

>> I'm following Scott Meyers' recommendation to use const
>> whenever possible (Effective C++ Third Edition, item 3).

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

Beware, it might be item 1 in the next edition  ;-)

> 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!".

The 'const' just prevents resetting the pointers stored within the Ui 
object. I think that's already beneficial.

> And unless setupUi() and retranslateUi() are made const functions,
> it means that these cannot be called if the Ui object is a const
> reference.

setupUi(parent) should definitely remain a non-const member function. Of 
course. It resets the pointers inside the Ui object. A 'const' reference to 
the Ui object should prevent setupUi(parent) calls after its initial setup.


Kind regards, Niels
--
Niels Dekker
http://www.xs4all.nl/~nd/dekkerware
Scientific programmer at LKEB, Leiden University Medical Center 




More information about the Qt-interest-old mailing list