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();
}
@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]
-
That sounds like a verbatim copy of a assignment;-)
-
My answer is:
- 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? :)
-
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?
-
You got the first one wrong... The code is OK as is I think.