Ask Help from beginner
Hi everybody, I do not know if this is the right place to post my question.
Anyway this is the issue.
I am beginner Qt and C++ amd so I have a lot of doubts.
using the wizzard of Qt I saw a few differnt in creating code but I did not find any explanation in Qt guide:
If i write: Qwidget w; I obtain an Object w with all methods and Propertis and I can acces them with ( . )
I mean w.show() same for QApplication
but when I create for example a label, I write: QLabel Lb=new QLabel();
Can someone explain me:
1 – which is the right way to declare an object
2 – what is the difference between these 2 modes
3 – where can I read the right way in Qt guide?
I mean ( point 3 ) when I consult the Qt guide and I check this or other classes, How cab I understand if
I have to use this modality Class_name Object_Name;
Class_name Object_name= new Class_name();
Please help me
Thanks a lot
Read a book on C++. It will answer your question. You are asking about the concept of stack and heap based objects, so check the section on pointers. Explaining this here is going a bit far.
Rule of thumb: Use the stack whenever possible (automatic cleanup of objects). Whenever using a QObject derived class using the stack is not possible and you need to use the heap, working with pointers.
You should follow the Tobias suggestion, having a solid C++ background is pretty useful when learning Qt.
Probably you already know it but, the Thinking in C++ [mindview.net] is a great book that you can get download for free.
Anyway the Qt Object Model is a subject on its own, that needs its own explanation, a nice one (with pretty figures :D), is in the L2 “Presentation with notes” of the Qt Learning material [qt.nokia.com], altough this material is intended to be used for teaching Qt I think it’s clear enough even for the beginners.
And if you’re looking for more detailed explanations check the Qt eLearning videos [qt.nokia.com], specially the Qt Training modules [qt.nokia.com] your question is answered in the “Objects int Qt” ones.