Closing a parent from a child
-
Hi. I am having issues closing a parent from a child. Specifically, the issue I am running into is this, from passwordbox.cpp:
connect(this, SIGNAL(destroyed()), this->parent(), SLOT(close()));
I am attempting to close a parent "CalibrationSelect" from within a child "PasswordBox" when the password box is deleted without an input submitted. So far, I am unsure why this not signalling. Per my understanding, the destroyed signal should be released when PasswordBox is closed, meaning that the parent should receive the close slot.
I have not encountered any messages telling me this->parent does not have the close slot. Please let me know if more info is required.
I have the following code:
passwordbox.h
#ifndef PASSWORDBOX_H #define PASSWORDBOX_H #include <QDialog> namespace Ui { class PasswordBox; } class PasswordBox : public QDialog { Q_OBJECT public: explicit PasswordBox(QWidget *parent = nullptr, int type = 0); ~PasswordBox(); enum{CalibMenu, ManualCalib}; signals: void passwordCorrect(); private slots: void passwordSubmitted(); void passwordBoxClosed(int result); private: Ui::PasswordBox *ui; int parentIndex; //Remembers who's the parent, like 23andme }; #endif // PASSWORDBOX_H
passwordbox.cpp
#include "passwordbox.h" #include "ui_passwordbox.h" #include "controller.h" #include <qDebug> const QStringList truePassword = {"CCCFP_SERVICE", "CCCFP_MANUAL"}; PasswordBox::PasswordBox(QWidget *parent, int type) : QDialog(parent), ui(new Ui::PasswordBox) { ui->setupUi(this); this->setWindowFlag(Qt::WindowContextHelpButtonHint, false); //this->setWindowTitle("Input password"); //Read password connect(ui->boxButton, SIGNAL(accepted()), this, SLOT(passwordSubmitted())); connect(this, SIGNAL(passwordCorrect()), this, SLOT(accept())); connect(this, SIGNAL(finished(int)), this, SLOT(passwordBoxClosed(int))); parentIndex = type; //Connect accepted to a seperate function too qDebug() << "pwBox"; } PasswordBox::~PasswordBox() { delete ui; } //Reads submitted pw void PasswordBox::passwordSubmitted(){ QString password = ui->passwordInput->text(); if (password == truePassword.at(parentIndex)){ qDebug() << "Yay! Password correct!"; Controller::get()->passwordUnlock(parentIndex); //unlocks the calibration stuff in controller emit passwordCorrect(); } else{ ui->boxText->setText("Password incorrect: please reinput"); ui->passwordInput->clear(); } } //closes pwbox void PasswordBox::passwordBoxClosed(int result){ qDebug() << result; disconnect(this, SIGNAL(finished(int)), this, SLOT(passwordBoxClosed(int))); if(result == QDialog::Rejected){ connect(this, SIGNAL(destroyed()), this->parent(), SLOT(close())); qDebug() << "closed:"; //this->parent()->close(); } this->close(); }
calibrationselect.cpp
//Set password box void CalibrationSelect::setPasswordBox(){ PasswordBox *passwordbox = new PasswordBox((QWidget*) this, PasswordBox::CalibMenu); connect(passwordbox, SIGNAL(passwordCorrect()), this, SLOT(unlockScreens())); passwordbox->open(); //NOTE Switch to open if possible. }
calibrationselect.h
#ifndef CALIBRATIONSELECT_H #define CALIBRATIONSELECT_H #include "controller.h" #include <QDialog> #include "cccfpmenubar.h" #include "passwordbox.h" namespace Ui { class CalibrationSelect; } class CalibrationSelect : public QDialog { Q_OBJECT public: explicit CalibrationSelect(QWidget *parent = nullptr); ~CalibrationSelect(); signals: void changeColor_signal(); void passwordCorrect(); private slots: void on_pushButton_Return_clicked(); void on_pushButton_CupCal_clicked(); void on_pushButton_LidCal_clicked(); void on_pushButton_PressureCal_clicked(); void on_pushButton_PumpCal_clicked(); void on_pushButton_MotorCal_clicked(); void on_pushButton_FactorySettings_clicked(); void on_pushButton_StirrerCal_clicked(); void on_pushButton_airCal_clicked(); void passwordSubmitted(); //Function that reads the contents of the pw void passwordBoxClosed(int); void unlockScreens(); //Unlock calibration screens private: Ui::CalibrationSelect *ui; void setPasswordBox(); //Set password box PasswordBox passwordbox; //((QWidget*) this, PasswordBox::CalibMenu) //void closeEvent(QCloseEvent*); }; #endif // CALIBRATIONSELECT_H
-
Hi. I am having issues closing a parent from a child. Specifically, the issue I am running into is this, from passwordbox.cpp:
connect(this, SIGNAL(destroyed()), this->parent(), SLOT(close()));
I am attempting to close a parent "CalibrationSelect" from within a child "PasswordBox" when the password box is deleted without an input submitted. So far, I am unsure why this not signalling. Per my understanding, the destroyed signal should be released when PasswordBox is closed, meaning that the parent should receive the close slot.
I have not encountered any messages telling me this->parent does not have the close slot. Please let me know if more info is required.
I have the following code:
passwordbox.h
#ifndef PASSWORDBOX_H #define PASSWORDBOX_H #include <QDialog> namespace Ui { class PasswordBox; } class PasswordBox : public QDialog { Q_OBJECT public: explicit PasswordBox(QWidget *parent = nullptr, int type = 0); ~PasswordBox(); enum{CalibMenu, ManualCalib}; signals: void passwordCorrect(); private slots: void passwordSubmitted(); void passwordBoxClosed(int result); private: Ui::PasswordBox *ui; int parentIndex; //Remembers who's the parent, like 23andme }; #endif // PASSWORDBOX_H
passwordbox.cpp
#include "passwordbox.h" #include "ui_passwordbox.h" #include "controller.h" #include <qDebug> const QStringList truePassword = {"CCCFP_SERVICE", "CCCFP_MANUAL"}; PasswordBox::PasswordBox(QWidget *parent, int type) : QDialog(parent), ui(new Ui::PasswordBox) { ui->setupUi(this); this->setWindowFlag(Qt::WindowContextHelpButtonHint, false); //this->setWindowTitle("Input password"); //Read password connect(ui->boxButton, SIGNAL(accepted()), this, SLOT(passwordSubmitted())); connect(this, SIGNAL(passwordCorrect()), this, SLOT(accept())); connect(this, SIGNAL(finished(int)), this, SLOT(passwordBoxClosed(int))); parentIndex = type; //Connect accepted to a seperate function too qDebug() << "pwBox"; } PasswordBox::~PasswordBox() { delete ui; } //Reads submitted pw void PasswordBox::passwordSubmitted(){ QString password = ui->passwordInput->text(); if (password == truePassword.at(parentIndex)){ qDebug() << "Yay! Password correct!"; Controller::get()->passwordUnlock(parentIndex); //unlocks the calibration stuff in controller emit passwordCorrect(); } else{ ui->boxText->setText("Password incorrect: please reinput"); ui->passwordInput->clear(); } } //closes pwbox void PasswordBox::passwordBoxClosed(int result){ qDebug() << result; disconnect(this, SIGNAL(finished(int)), this, SLOT(passwordBoxClosed(int))); if(result == QDialog::Rejected){ connect(this, SIGNAL(destroyed()), this->parent(), SLOT(close())); qDebug() << "closed:"; //this->parent()->close(); } this->close(); }
calibrationselect.cpp
//Set password box void CalibrationSelect::setPasswordBox(){ PasswordBox *passwordbox = new PasswordBox((QWidget*) this, PasswordBox::CalibMenu); connect(passwordbox, SIGNAL(passwordCorrect()), this, SLOT(unlockScreens())); passwordbox->open(); //NOTE Switch to open if possible. }
calibrationselect.h
#ifndef CALIBRATIONSELECT_H #define CALIBRATIONSELECT_H #include "controller.h" #include <QDialog> #include "cccfpmenubar.h" #include "passwordbox.h" namespace Ui { class CalibrationSelect; } class CalibrationSelect : public QDialog { Q_OBJECT public: explicit CalibrationSelect(QWidget *parent = nullptr); ~CalibrationSelect(); signals: void changeColor_signal(); void passwordCorrect(); private slots: void on_pushButton_Return_clicked(); void on_pushButton_CupCal_clicked(); void on_pushButton_LidCal_clicked(); void on_pushButton_PressureCal_clicked(); void on_pushButton_PumpCal_clicked(); void on_pushButton_MotorCal_clicked(); void on_pushButton_FactorySettings_clicked(); void on_pushButton_StirrerCal_clicked(); void on_pushButton_airCal_clicked(); void passwordSubmitted(); //Function that reads the contents of the pw void passwordBoxClosed(int); void unlockScreens(); //Unlock calibration screens private: Ui::CalibrationSelect *ui; void setPasswordBox(); //Set password box PasswordBox passwordbox; //((QWidget*) this, PasswordBox::CalibMenu) //void closeEvent(QCloseEvent*); }; #endif // CALIBRATIONSELECT_H
Apart from the strange design (why would a child need to know something about a parent - you already used it the correct other way around for the correct password so why another way in this case?) - I don't see where you delete your PasswordBox dialog - you only close it.
-
Apart from the strange design (why would a child need to know something about a parent - you already used it the correct other way around for the correct password so why another way in this case?) - I don't see where you delete your PasswordBox dialog - you only close it.
@Christian-Ehrlicher Thank you, I was unaware that "deleting" in Qt and "closing" are two different concepts. In any case, I am attempting to close a parent "CalibrationSelect" from within a child "PasswordBox" when the password box is closed without an input submitted.
Slightly unrelated, the reason for my design is because I want to use passwordbox as it's own unit to be added to other files too, and I want to keep as many things within passwordbox class as possible.
-
Hi. I am having issues closing a parent from a child. Specifically, the issue I am running into is this, from passwordbox.cpp:
connect(this, SIGNAL(destroyed()), this->parent(), SLOT(close()));
I am attempting to close a parent "CalibrationSelect" from within a child "PasswordBox" when the password box is deleted without an input submitted. So far, I am unsure why this not signalling. Per my understanding, the destroyed signal should be released when PasswordBox is closed, meaning that the parent should receive the close slot.
I have not encountered any messages telling me this->parent does not have the close slot. Please let me know if more info is required.
I have the following code:
passwordbox.h
#ifndef PASSWORDBOX_H #define PASSWORDBOX_H #include <QDialog> namespace Ui { class PasswordBox; } class PasswordBox : public QDialog { Q_OBJECT public: explicit PasswordBox(QWidget *parent = nullptr, int type = 0); ~PasswordBox(); enum{CalibMenu, ManualCalib}; signals: void passwordCorrect(); private slots: void passwordSubmitted(); void passwordBoxClosed(int result); private: Ui::PasswordBox *ui; int parentIndex; //Remembers who's the parent, like 23andme }; #endif // PASSWORDBOX_H
passwordbox.cpp
#include "passwordbox.h" #include "ui_passwordbox.h" #include "controller.h" #include <qDebug> const QStringList truePassword = {"CCCFP_SERVICE", "CCCFP_MANUAL"}; PasswordBox::PasswordBox(QWidget *parent, int type) : QDialog(parent), ui(new Ui::PasswordBox) { ui->setupUi(this); this->setWindowFlag(Qt::WindowContextHelpButtonHint, false); //this->setWindowTitle("Input password"); //Read password connect(ui->boxButton, SIGNAL(accepted()), this, SLOT(passwordSubmitted())); connect(this, SIGNAL(passwordCorrect()), this, SLOT(accept())); connect(this, SIGNAL(finished(int)), this, SLOT(passwordBoxClosed(int))); parentIndex = type; //Connect accepted to a seperate function too qDebug() << "pwBox"; } PasswordBox::~PasswordBox() { delete ui; } //Reads submitted pw void PasswordBox::passwordSubmitted(){ QString password = ui->passwordInput->text(); if (password == truePassword.at(parentIndex)){ qDebug() << "Yay! Password correct!"; Controller::get()->passwordUnlock(parentIndex); //unlocks the calibration stuff in controller emit passwordCorrect(); } else{ ui->boxText->setText("Password incorrect: please reinput"); ui->passwordInput->clear(); } } //closes pwbox void PasswordBox::passwordBoxClosed(int result){ qDebug() << result; disconnect(this, SIGNAL(finished(int)), this, SLOT(passwordBoxClosed(int))); if(result == QDialog::Rejected){ connect(this, SIGNAL(destroyed()), this->parent(), SLOT(close())); qDebug() << "closed:"; //this->parent()->close(); } this->close(); }
calibrationselect.cpp
//Set password box void CalibrationSelect::setPasswordBox(){ PasswordBox *passwordbox = new PasswordBox((QWidget*) this, PasswordBox::CalibMenu); connect(passwordbox, SIGNAL(passwordCorrect()), this, SLOT(unlockScreens())); passwordbox->open(); //NOTE Switch to open if possible. }
calibrationselect.h
#ifndef CALIBRATIONSELECT_H #define CALIBRATIONSELECT_H #include "controller.h" #include <QDialog> #include "cccfpmenubar.h" #include "passwordbox.h" namespace Ui { class CalibrationSelect; } class CalibrationSelect : public QDialog { Q_OBJECT public: explicit CalibrationSelect(QWidget *parent = nullptr); ~CalibrationSelect(); signals: void changeColor_signal(); void passwordCorrect(); private slots: void on_pushButton_Return_clicked(); void on_pushButton_CupCal_clicked(); void on_pushButton_LidCal_clicked(); void on_pushButton_PressureCal_clicked(); void on_pushButton_PumpCal_clicked(); void on_pushButton_MotorCal_clicked(); void on_pushButton_FactorySettings_clicked(); void on_pushButton_StirrerCal_clicked(); void on_pushButton_airCal_clicked(); void passwordSubmitted(); //Function that reads the contents of the pw void passwordBoxClosed(int); void unlockScreens(); //Unlock calibration screens private: Ui::CalibrationSelect *ui; void setPasswordBox(); //Set password box PasswordBox passwordbox; //((QWidget*) this, PasswordBox::CalibMenu) //void closeEvent(QCloseEvent*); }; #endif // CALIBRATIONSELECT_H
@Dummie1138
I echo @Christian-Ehrlicher's comment, do you really want to close a parent from a child? Like in the real world, it should not be allowed to do that. I can't imagine a scenario where it's a good idea. Why can't parent close itself when child closes if that's what's wanted? For example, instead ofconnect(this, SIGNAL(destroyed()), this->parent(), SLOT(close()));
it would be much more acceptable if parent did a connection like
connect(child, &ChildClass::destroyed, this, &ParentClass::close); // or use eventFilter() from parent // or subclass ChildClass and emit signal from closeEvent()