[Development] unique_ptr and Qt, Take 2

Allan Sandfeld Jensen kde at carewolf.com
Fri May 3 21:32:01 CEST 2019

On Freitag, 3. Mai 2019 20:24:07 CEST Thiago Macieira wrote:
> On Friday, 3 May 2019 10:22:20 PDT Daniel Teske wrote:
> > std::unique_ptr<QPushButton> rightButton =
> > std::make_unique<QPushButton>("RIGHT");
> > layout->addWidget(std::move(rightButton));
> The problem in this particular example is that once you've added the widget,
> the rightButton smart pointer no longer has a pointer. You can't continue
> to set up your push button. In most cases, this is just a matter of moving
> the set up before the addition / reparenting, or using the other idiom
> where the object is never in a smart pointer in the first place.
> So this begs the question of whether std::unique_ptr is the best smart
> pointer for this scenario. Would it make sense to create one that
> understands parent- child relationship?

I would prefer that too. Something like a QPointer except it takes strong 
instead of weak reference when holding objects without a parent. This means 
you could pass it to an object taking ownership, and it would transition from 
strong to weak, but still be valid.

Alternatively QObjects should be referenced counted, one reference could be 
from a parent, but you could also strong reference with an explicit shared 


More information about the Development mailing list