QDialog with close button and X button
-
wrote on 20 Aug 2021, 15:13 last edited by Abhi_Varma
I'm new to Qt, facing some issue with dialogs. Here is my basic UI design I have a Class TabA that inherits QWidget (it is a tab in a window, there are multiple tabs like tabA, tabB...) . In the tab there is a button call "Send" on clicking it a custom dialog is created which shows some progress(lets call it MyDialog). The dialog also has a "close " button at the bottom on clicking the button it will close the dialog. Below are my member functions of TabA class.
CreateMyDialog() slot will take care of creating the custom dialog when we click that "Send" button. You also see a signal slot connection in CreateMyDialog() where I connected rejected() signal to a slot, this rejected() signal is emitted (correct me if I'm wrong) when user clicks the "close" button or the X button on the top right corner of the dialog. When this signal is emitted I basically want to destroy the MyDialog class object. I read that on clicking the "X" button closeEvent() is called and at the end QDialog::rejected() is emitted. The "close" button I have on clicking that MyDialog::On_btnClose_Clicked() slot is triggered which will call the close() which I'm assuming that at a later stage will also emit QDialog::rejected() because I notice the corresponding slot OnMyDialogClosed() is called.
void CreateMyDialog() { MyDialog *pMyDialog = new MyDialog(this); connect(pMyDialog, SIGNAL(rejected()), this, SLOT(OnMyDialogClosed()); pMyDialog->setModel(true); pMyDialog->show(); }
The Following slot is used to destroy the MyDialog variable
void TabA ::OnMyDialogClosed() { if(pMyDialog != NULL) delete MyDialog; }
Class MyDialog: public QDialog { }
MyDialog has one button called "cancel" on clicking it following slot will be invoked.
void MyDialog::On_btnClose_Clicked() { close(); }
Even though on clicking either "X" button or "close" the necessary slot is invoked, its also giving me exceptions. Any idea where I made a mistake
-
wrote on 20 Aug 2021, 16:50 last edited by
Please use punctuations in the appropriate places since this is difficult to read.
Instead of QDialog::Rejected, it returns QDialog::Accepted? -
Please use punctuations in the appropriate places since this is difficult to read.
Instead of QDialog::Rejected, it returns QDialog::Accepted?wrote on 21 Aug 2021, 12:48 last edited by@stretchthebits It is emitting rejected() in both cases but getting exceptions. I have edited my post kindly recheck
-
Hi
Just as a note
You give the dialog a parent
MyDialog *pMyDialog = new MyDialog(this);so also using delete MyDialog; is a no go as
Qt has an owner system and you risk double deletes
https://doc.qt.io/qt-5/objecttrees.htmlSo basically Dont give it a parent and use delete or
let Qt clean it up.Also please note the flag
diag->setAttribute(Qt::WA_DeleteOnClose);that you can set so the dialog will auto delete (when closed) when its not optimal to let Qt do it later via the parent assignment. Like long running app and you create this dialog many times.
1/4