[Interest] What's the recommended way to put QObjects in smart pointers?

Konstantin Tokarev annulen at yandex.ru
Thu May 5 12:20:07 CEST 2016



05.05.2016, 13:16, "Jean-Michaël Celerier" <jeanmichael.celerier at gmail.com>:
> On Thu, May 5, 2016 at 3:24 AM, Nikos Chantziaras <realnc at gmail.com> wrote:
>> By now, I consider application-level code that uses 'new' or 'delete' as bogus and in need of fixing.
> This line of thought doesn't work at all with Qt. It's just how it is done. If you are in such dire need of guaranteeing the absence of memory problems :
>
> template<typename T, typename... Args>
> auto make_qobject(Args&&... args, QObject* parent) // Repeat for QGraphicsItem / Object if necessary
> {
>     assert(parent);
>     return new T{std::forward<Args>(args)..., parent};
> }
>
> QMainWindow win;
> auto safe_obj_1 = make_qobject<QListWidget> (&win);
> auto safe_obj_2 = make_qobject<QGridView>(&win);

Nevertheless that won't save from memory problems when parent needs to have longer lifetime then its children


-- 
Regards,
Konstantin



More information about the Interest mailing list