C++ QObject’s Child Management



  • @
    #include <QApplication>
    #include <QLabel>
    #include <QVBoxLayout>
    #include <QTextEdit>
    #include <QWidget>

    int main (int argc, char* argv[]) {
    QApplication app (argc, argv);
    QWidget window;

    QLabel* label = new QLabel("Please enter some text");
    QTextEdit* textEdit = new QTextEdit;
    
    QVBoxLayout* layout = new QVBoxLayout;
    layout->addwidget(label);
    layout->addwidget(textEdit);
    window.setLayout(layout);
    window.show;
    
    return app.exec&#40;&#41;;
    

    }
    @

    Questions:

    1.Qt provides a child management facility through the QObject class. Where is the QObject in the program above? Explain

    2.The QObject class provides the function setParent(QObject *parent) to specify a Qbject to be its parent. Why is this function not use in this program?

    3.The program uses both heap and stack objects. Explain how the parnt-child facility works when the:

    a.parent is a heap obhect and the child objects are stack objects
    b.parent is a stack obhect and the child objects are heap objects

    [Edit:Please, wrap code in @ tags /Vass]


  • Moderators

    That sounds like a verbatim copy of a assignment;-)



  • no it is exam revision



  • My answer is:

    1. For the child management, the QObject has to pass reference to the child, for example:

    @QWidget window;
    QLabel* label = new QLabel("Please enter some text",&window);
    QTextEdit* textEdit = new QTextEdit(&window);
    QVBoxLayout* layout = new QVBoxLayout(&window);@

    in this way they are registered in a window object, and whenever window is deleted, those objects will be deleted as well.

    I want to make sure is correct



  • You are so lazy.. even to use "code" brackets? :)


  • Moderators

    Why are you not asking the people that are paid to teach you?

    How about proposing an answer for us to discuss?

    Where are your questions on the topic?


  • Moderators

    You got the first one wrong... The code is OK as is I think.


Log in to reply
 

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