#includeint main(int argc, char *argv[]) { QApplication app(argc, argv); QMainWindow *parent = new QMainWindow(); QMainWindow *child = new QMainWindow(parent); // child is now a child of parent // parent deletes child when it is destroyed return app.exec(); }
#includeIn both examples, we are using the Qt library's QObject class and its children() method to establish and manipulate parent-child relationships between objects.#include #include int main(int argc, char *argv[]) { QApplication app(argc, argv); QMainWindow *parent = new QMainWindow(); QCheckBox *checkbox1 = new QCheckBox("Checkbox 1", parent); QCheckBox *checkbox2 = new QCheckBox("Checkbox 2", parent); // disconnect the clicked signal from a slot for all child objects of parent const QObjectList children = parent->children(); for (QObject *child : children) { QCheckBox *checkbox = qobject_cast (child); if (checkbox) { QObject::disconnect(checkbox, SIGNAL(clicked(bool)), 0, 0); } } return app.exec(); }