[Development] parented_ptr

Marc Mutz marc.mutz at qt.io
Wed Oct 29 17:32:57 CET 2025


On 29.10.25 16:55, Volker Hilsheimer via Development wrote:
> I.e. today I often write
> 
> QDialog *dialog = new QDialog;
> QLabel *label = new QLabel(tr(“User name:”));
> QLineEdit *input = new QLineEdit;

Please, don't spread this anti-pattern further. It was a very sad day 
when QLayout was changed to fix up parents silently. And it was very 
hard to teach up against this anti-pattern, esp. since Trolltech rolled 
it our over all their examples at the same time.

Just never create a child QObject w/o the final parent, and everything 
will be peachy.

The code above is brittle (if the maintenance programmer puts in an 
early return somewhere, this leaks), less readable (I know they will be 
children of each other, but I don't see that, so I need to go look for 
it elsewhere), and - potentially - less efficient (two states instead of 
one).

QLayout should _complain_ when the parent is wrong, not silently fix it.

Thanks,
Marc

-- 
Marc Mutz  (he/his)
Principal Software Engineer

The Qt Company
Erich-Thilo-Str. 10 12489
Berlin, Germany
www.qt.io

Geschäftsführer: Mika Pälsi, Juha Varelius, Jouni Lintunen
Sitz der Gesellschaft: Berlin,
Registergericht: Amtsgericht Charlottenburg,
HRB 144331 B



More information about the Development mailing list