How To Detect if the User Closed the window?
-
wrote on 12 Aug 2024, 18:26 last edited by
I tried searching YouTube to find how to detect if the user clicked on the x button to close the window, but didn't manage to find, does anyone know how to do it in qt c++
-
-
wrote on 13 Aug 2024, 13:59 last edited by Yousef Alaa Hussain
@Christian-Ehrlicher I am a beginner and I didn't understand that much what is written in the doc
-
Override this function and do whatever you want to do in there.
-
@Christian-Ehrlicher I am a beginner and I didn't understand that much what is written in the doc
wrote on 13 Aug 2024, 14:04 last edited by JonB@Yousef-Alaa-Hussain
Qt does not issue a signal on closing a window/widget. However it does call acloseEvent()
method in any widget. If you want to capture/act on that you should subclass whicheverQWidget
-derived the "window" is so that you can override that (virtual protected
) method.If you really don't want to subclass you could alternatively install an event filter and look for the
QEvent::close
event.Both of these are illustrated in e.g. https://stackoverflow.com/questions/17480984/how-do-i-handle-the-event-of-the-user-pressing-the-x-close-button. It also shows using
aboutToQuit()
if all you really want to know is when the whole application is being closed. -
@Yousef-Alaa-Hussain
Qt does not issue a signal on closing a window/widget. However it does call acloseEvent()
method in any widget. If you want to capture/act on that you should subclass whicheverQWidget
-derived the "window" is so that you can override that (virtual protected
) method.If you really don't want to subclass you could alternatively install an event filter and look for the
QEvent::close
event.Both of these are illustrated in e.g. https://stackoverflow.com/questions/17480984/how-do-i-handle-the-event-of-the-user-pressing-the-x-close-button. It also shows using
aboutToQuit()
if all you really want to know is when the whole application is being closed.wrote on 13 Aug 2024, 14:42 last edited by@JonB thank you so much for your answer
and i am really sorry for asking a lot of questions but,
as you can see here i have a function here that has a simple label and text input field and two buttons save and cancelvoid MainWindow::WriteAnyButtonClicked() { QWidget* WriteAnyWindow = new QWidget(); QVBoxLayout* MainLayout = new QVBoxLayout(); // Top Layout QHBoxLayout* TopLayout = new QHBoxLayout(); QLabel* EnterAnyLabel = new QLabel("Enter What You Want To Share: "); EnterAnyLabel->setAlignment(Qt::AlignLeft); EnterAnyLabel->setAlignment(Qt::AlignVCenter); TopLayout->addWidget(EnterAnyLabel); //Middle Layout QHBoxLayout* MiddleLayout = new QHBoxLayout(); QTextEdit* EnterAnyTextEdit = new QTextEdit(); EnterAnyTextEdit->setFocus(); MiddleLayout->addWidget(EnterAnyTextEdit); // Bottom Layout QHBoxLayout* BottomLayout = new QHBoxLayout(); QPushButton* Save_Button = new QPushButton("Save"); Save_Button->setFixedSize(100, 50); QPushButton* Exit_Button = new QPushButton("Cancel 🇽"); Exit_Button->setFixedSize(50, 50); BottomLayout->addWidget(Save_Button); BottomLayout->addWidget(Exit_Button); MainLayout ->addLayout(TopLayout); MainLayout ->addLayout(MiddleLayout); MainLayout ->addLayout(BottomLayout); WriteAnyWindow->setLayout(MainLayout); WriteAnyWindow->setWindowTitle("Send By Text"); WriteAnyWindow->setFixedSize(400,400); WriteAnyWindow->show(); // Button Connects connect(Exit_Button,&QPushButton::clicked, this,[&](){delete WriteAnyWindow;}); }
and i want to check when this widget is closed to automatically save the input that the user entered in the textinput,
do you know how to do this? do i really need to rearrange all of this code to an entire sperate class just to get this protected close event funtion and then create a funtion for it like this:void MainWindow::closeEvent (QCloseEvent *event) { QMessageBox::StandardButton resBtn = QMessageBox::question( this, "The Sender", tr("Are you sure?\n"), QMessageBox::Cancel | QMessageBox::No | QMessageBox::Yes, ,QMessageBox::Cancel); if (resBtn != QMessageBox::Yes) { event->ignore(); } else { event->accept(); } }
really?
and thanks! -
@JonB thank you so much for your answer
and i am really sorry for asking a lot of questions but,
as you can see here i have a function here that has a simple label and text input field and two buttons save and cancelvoid MainWindow::WriteAnyButtonClicked() { QWidget* WriteAnyWindow = new QWidget(); QVBoxLayout* MainLayout = new QVBoxLayout(); // Top Layout QHBoxLayout* TopLayout = new QHBoxLayout(); QLabel* EnterAnyLabel = new QLabel("Enter What You Want To Share: "); EnterAnyLabel->setAlignment(Qt::AlignLeft); EnterAnyLabel->setAlignment(Qt::AlignVCenter); TopLayout->addWidget(EnterAnyLabel); //Middle Layout QHBoxLayout* MiddleLayout = new QHBoxLayout(); QTextEdit* EnterAnyTextEdit = new QTextEdit(); EnterAnyTextEdit->setFocus(); MiddleLayout->addWidget(EnterAnyTextEdit); // Bottom Layout QHBoxLayout* BottomLayout = new QHBoxLayout(); QPushButton* Save_Button = new QPushButton("Save"); Save_Button->setFixedSize(100, 50); QPushButton* Exit_Button = new QPushButton("Cancel 🇽"); Exit_Button->setFixedSize(50, 50); BottomLayout->addWidget(Save_Button); BottomLayout->addWidget(Exit_Button); MainLayout ->addLayout(TopLayout); MainLayout ->addLayout(MiddleLayout); MainLayout ->addLayout(BottomLayout); WriteAnyWindow->setLayout(MainLayout); WriteAnyWindow->setWindowTitle("Send By Text"); WriteAnyWindow->setFixedSize(400,400); WriteAnyWindow->show(); // Button Connects connect(Exit_Button,&QPushButton::clicked, this,[&](){delete WriteAnyWindow;}); }
and i want to check when this widget is closed to automatically save the input that the user entered in the textinput,
do you know how to do this? do i really need to rearrange all of this code to an entire sperate class just to get this protected close event funtion and then create a funtion for it like this:void MainWindow::closeEvent (QCloseEvent *event) { QMessageBox::StandardButton resBtn = QMessageBox::question( this, "The Sender", tr("Are you sure?\n"), QMessageBox::Cancel | QMessageBox::No | QMessageBox::Yes, ,QMessageBox::Cancel); if (resBtn != QMessageBox::Yes) { event->ignore(); } else { event->accept(); } }
really?
and thanks!wrote on 13 Aug 2024, 14:48 last edited by@Yousef-Alaa-Hussain
As I wrote there are two possibilities.-
Subclass and override
closeEvent()
. Your code looks reasonable. -
If you do not want to "rearrange all of this code to an entire sperate class" (I don't see the problem, you seem to have already done it) then you can do it by installing an event filter.
I already wrote just this and referred you to a link showing both alternatives. I am not sure what you are actually asking now.
-
-
@Yousef-Alaa-Hussain
As I wrote there are two possibilities.-
Subclass and override
closeEvent()
. Your code looks reasonable. -
If you do not want to "rearrange all of this code to an entire sperate class" (I don't see the problem, you seem to have already done it) then you can do it by installing an event filter.
I already wrote just this and referred you to a link showing both alternatives. I am not sure what you are actually asking now.
wrote on 13 Aug 2024, 16:03 last edited by@JonB said in How To Detect if the User Closed the window?:
I don't see the problem, you seem to have already done it
actually, this close event is for the entire window (the main window) but
if the user clicks a button inside this main window, another window will pop up, and I wanted to detect when the user closes the new window that popped up after the button got clicked,I guess I need to make an entire new class, uh, qt is very hard as a beginner, I hope I get out of the hell of beginner
So now I will try installing that I don't know thing you talked about, and I will see if I can adapt with it. If there is a tutorial out there for this thing I need to install, let's hope.
Thank you so much, and sorry for wasting your time
-
-
@JonB said in How To Detect if the User Closed the window?:
I don't see the problem, you seem to have already done it
actually, this close event is for the entire window (the main window) but
if the user clicks a button inside this main window, another window will pop up, and I wanted to detect when the user closes the new window that popped up after the button got clicked,I guess I need to make an entire new class, uh, qt is very hard as a beginner, I hope I get out of the hell of beginner
So now I will try installing that I don't know thing you talked about, and I will see if I can adapt with it. If there is a tutorial out there for this thing I need to install, let's hope.
Thank you so much, and sorry for wasting your time
wrote on 13 Aug 2024, 17:03 last edited byanother window will pop up, and I wanted to detect when the user closes the new window that popped up
if the "window which pops up" is a (modal) dialog then you are told when it is closed. If not a modal dialog then you will have to subclass the window/widget or use an event filter.
-
@JonB thank you so much for your answer
and i am really sorry for asking a lot of questions but,
as you can see here i have a function here that has a simple label and text input field and two buttons save and cancelvoid MainWindow::WriteAnyButtonClicked() { QWidget* WriteAnyWindow = new QWidget(); QVBoxLayout* MainLayout = new QVBoxLayout(); // Top Layout QHBoxLayout* TopLayout = new QHBoxLayout(); QLabel* EnterAnyLabel = new QLabel("Enter What You Want To Share: "); EnterAnyLabel->setAlignment(Qt::AlignLeft); EnterAnyLabel->setAlignment(Qt::AlignVCenter); TopLayout->addWidget(EnterAnyLabel); //Middle Layout QHBoxLayout* MiddleLayout = new QHBoxLayout(); QTextEdit* EnterAnyTextEdit = new QTextEdit(); EnterAnyTextEdit->setFocus(); MiddleLayout->addWidget(EnterAnyTextEdit); // Bottom Layout QHBoxLayout* BottomLayout = new QHBoxLayout(); QPushButton* Save_Button = new QPushButton("Save"); Save_Button->setFixedSize(100, 50); QPushButton* Exit_Button = new QPushButton("Cancel 🇽"); Exit_Button->setFixedSize(50, 50); BottomLayout->addWidget(Save_Button); BottomLayout->addWidget(Exit_Button); MainLayout ->addLayout(TopLayout); MainLayout ->addLayout(MiddleLayout); MainLayout ->addLayout(BottomLayout); WriteAnyWindow->setLayout(MainLayout); WriteAnyWindow->setWindowTitle("Send By Text"); WriteAnyWindow->setFixedSize(400,400); WriteAnyWindow->show(); // Button Connects connect(Exit_Button,&QPushButton::clicked, this,[&](){delete WriteAnyWindow;}); }
and i want to check when this widget is closed to automatically save the input that the user entered in the textinput,
do you know how to do this? do i really need to rearrange all of this code to an entire sperate class just to get this protected close event funtion and then create a funtion for it like this:void MainWindow::closeEvent (QCloseEvent *event) { QMessageBox::StandardButton resBtn = QMessageBox::question( this, "The Sender", tr("Are you sure?\n"), QMessageBox::Cancel | QMessageBox::No | QMessageBox::Yes, ,QMessageBox::Cancel); if (resBtn != QMessageBox::Yes) { event->ignore(); } else { event->accept(); } }
really?
and thanks!wrote on 13 Aug 2024, 17:13 last edited by Pl45m4@Yousef-Alaa-Hussain said in How To Detect if the User Closed the window?:
void MainWindow::WriteAnyButtonClicked()
This function creates new widgets and layouts every time it's called (on button click, it seems).
You know that, right?!
It also leaks memory since you are not using the features of theQObject
-tree nor deleting the pointers afterwards.
(Edit: youdelete WriteAnyWindow
forcefully, which is not recommended.)However, the
closeEvent
looks good, as @JonB already mentioned. -
Hi,
If I have understood things correctly, wouldn't QInputDialog::getText do what you want ?
1/11