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

Robert Hairgrove evorgriahr at hispeed.ch
Tue Feb 8 12:23:47 CET 2011


On Tue, 2011-02-08 at 12:04 +0100, Thiago Macieira wrote:
> Em terça-feira, 8 de fevereiro de 2011, às 11:50:03, Robert Hairgrove 
> escreveu:
> > If "ui" is const, you cannot call any of the non-const functions on any
> > of the GUI objects it contains ... such as setText() on an edit control,
> > for example. I think this would be severely limiting.
> 
> Hello Robert
> 
> That is not correct. If the "ui" object is const, its members are const. That 
> means if you a QTextEdit* as a member, the *pointer* becomes const, not the 
> pointed object.
> 
> So you can do this:
> 	ui->textEdit->setText("foo");
> 
> But you can't do this:
> 	ui->textEdit = new QTextEdit(this);
> 

True, I forgot this about pointers.

> > As to deletion, normally the parent of an object which inherits QObject
> > will delete its children automatically. I would think that creating ui
> > as a member object variable would lead to a double deletion, but I might
> > be mistaken.
> 
> No, there's no double deletion. The Ui_* classes have no constructor or 
> destructors. So they don't delete the form elements that setupUi creates. They 
> contain only pointers, so these classes are effectively POD.
> 
> However, the Ui class itself must be deleted if it was newed.

Re-reading the OP's message, it seems that his template function
CreateAndSetupUi is actually functioning as some kind of smart
pointer...but we'd have to see the code to know.

But is there a real advantage to doing it like this? That is the
question...




More information about the Qt-interest-old mailing list