I use Qt designer to design the UI. When implementing the design into the code, I use the multiple inheritance approach. There’s no real reason why I use this method, I just found it to be easier.
Anyway, when I looked into the generated header file I noticed in the setupUi() function that everything is allocated in heap. I don’t really need the objects to outlive it’s parent and according to this, in my case, it shouldn’t be allocated in heap.
In situations where the parent object is just a small modal dialog allocated in stack, wouldn’t it be a waste that it’s UI objects are allocated in heap?
Is there a work-around for this? Should I just stop worrying? I haven’t found a situation where this has become a problem but I’m still curious about it. In fact, it’s not a problem at all. I just want to know.
General Qt practice is to use heap allocation for any QObject unless it’s lifetime is limited to the current scope. This may seem wasteful, but in the context of building a UI any performance impact will be negligible.
Also note that due to Qt’s extensive use of the pimpl idiom, every created QObject has an internal QObjectPrivate which is always heap allocated, so it’s simply not possible to keep everything on the stack.
So I would suggest that you stop worrying. 🙂