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 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

  • Moderators

    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're right Tobias, with some exceptions to QObject, e.g. when using QFile - which is allocated on the stack even in most of the Qt docs and examples :-)

  • 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++": 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":, 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":, specially the "Qt Training modules": your question is answered in the "Objects int Qt" ones.

    Happy learning!

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.