Where are the .ui files?
-
@Kent-Dorfman said in Where are the .ui files?:
The transfer of object ownership in the parent child widget tree relies on it, and is why stack allocated QObjects are a bad idea.
What I'm trying to say is that in C++ it is generally a good idea to put variables on the stack and not on the heap. And you are right that we need to be careful with Qt. Everything in Qt that has a parent assigned needs to be heap allocated (and not managed by a smart pointer!). However, the QApplication and the top-level widget don't have a parent assigned. This means that in a large application no widget will ever be destroyed because the parent (i.e. the MainWindow in my example) is never destroyed. That's why I advocate for the top-level widget to be on the stack. Otherwise, you should call
app->exec()
, store its return value, explicitelydelete
the top-level widget, and finally return the return value from app->exec(). Using a stack variable is much easier.I am so vocal about it because I was taught to use pointers and
new
everywhere when I started with C++. Turns out that this was the source of most bugs. So, using stack variables almost everywhere (except for Qt's objects with parents assigned) helps to prevent a lot of bugs. (There are other reasons to use pointers related to OOP and polymorphism, but even then there are fewer cases than I was taught initially.)@SimonSchroeder said in Where are the .ui files?:
I am so vocal about it because I was taught to use pointers and new everywhere when I started with C++. Turns out that this was the source of most bugs. So, using stack variables almost everywhere (except for Qt's objects with parents assigned) helps to prevent a lot of bugs. (There are other reasons to use pointers related to OOP and polymorphism, but even then there are fewer cases than I was taught initially.)
True. This correlates also with my experience.
@Pl45m4 said in Where are the .ui files?:
Have seen beginners creating everything, Qt and non-Qt related stuff, on the heap regardless... that could need some optimization :)
Could be "bad" teachers, wrong guides or courses... or just the wrong assumptions that it needs to be done this way.
People have heard "C++ is hard", "C++ is complicated", "Lots of bad black memory/pointer magic you can do"... which is kinda accurate... so they take the sledgehammer to crack a nut :D
And maybe they get "inpired" by other OOP languages like Java, where you don't have to manage your memory yourself and new'ing something does not come with any drawback, therefore it's most used.