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

Thiago Macieira thiago at kde.org
Tue Feb 8 13:29:08 CET 2011


Em terça-feira, 8 de fevereiro de 2011, às 12:36:30, Niels Dekker escreveu:
> Thiago Macieira wrote:
> > Why a reference? What's the advantage here?
> 
> During the lifetime of the form (e.g., CalculatorForm), its ui data member
> should never be NULL. So in my opinion, a reference to the Ui object is more
> appropriate than a pointer.

Ok, that sounds fair.

> > And how do you delete the reference afterwards?
> 
> The Ui object is owned by the form. So it is deleted when the form is
> deleted.

That's not correct. Look at the valgrind output I've just sent in another 
email.

> >> CreateAndSetupUi(T&) is used as parent of the newly created Ui
> >> object, so it will take care of deallocation.
> > 
> > Uh... parent-child relationship only exists between QObject-derived
> > classes. The Ui object isn't a QObject, so it doesn't participate in
> > that.
> 
> Right. That's why I stored the Ui object inside a little helper class,
> derived from QObject. But that's an implementation detail of
> CreateAndSetupUi(T&). Are you interested to have a look at the entire code?

So your implementation is even more complex and costly... More or less like 
this, right?

template <typename T>
class UiHolder: public QObject
{
public:
    T ui;
    UiHolder(QObject *parent) : QObject(parent) {}
};

template <typename T, typename BaseClass>
const T &createAndSetupUi(BaseClass *parent)
{
    UiHolder<T> *ptr = new UiHolder<T>(parent);
    ptr->ui.setupUi(parent);
    return ptr->ui;
}

This creates a QObject (8 or 16 bytes, plus overhead), a QObjectPrivate (80 or 
144 bytes, plus overhead) and adds it to the parent's list of children.

> >> Actually the template function does not really return a reference to
> >> the created Ui object. It returns a proxy object, which converts
> >> implicitly to a (const) reference of the right Ui class type.
> > 
> > Why the complexity?
> 
> Because it very much simplifies the Ui creation in the form class, IMO.

I meant: why does it return a proxy object? Returning the reference is 
probably enough -- and it can be done on registers, instead of the caller 
setting up an area for return.

> I agree that a smart pointer is preferable to a raw pointer. But personally,
> I still prefer having ui as a reference. Is there anyone else here who
> prefers to have the Ui member variable as a reference?

You can have both, if you need, like this:

	ui_ptr(createAndSetupUi(this)),
	ui(*ui_ptr.data())

I personally would not waste the extra 4 or 8 bytes to keep the exact same 
bits. (much less the 100 or 200 bytes required for a QObject holder)

-- 
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
  Senior Product Manager - Nokia, Qt Development Frameworks
      PGP/GPG: 0x6EF45358; fingerprint:
      E067 918B B660 DBD1 105C  966C 33F5 F005 6EF4 5358
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 190 bytes
Desc: This is a digitally signed message part.
Url : http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20110208/fce1afeb/attachment.bin 


More information about the Qt-interest-old mailing list