Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
Terminating QProcess from Dialog
I have a base object that contains a creates a QProcess and a QDialog on the heap. The QProcess is started with QProcess::start() and at the same time the dialog is being displayed. As I use the finished() signal of the QProcess instead of the blocking QProcess::waitForFinished() routine I am able to interact with the dialog without any problems.
However, I want to kill the QProcess as soon as the dialog is being closed (when QDialog::destroyed()) is being emitted. My problem is that the destroyed() signal is emitted AFTER the QProcess finished although the dialog window disappears immediately when pressing the 'X' button.
Can somebody explain this behavior and tell me how I can get this fixed? The dialog itself does not know anything about the QProcess.
"closed" and "destroyed" are two different concepts.
When a dialog is "closed" it is not deleted, just hidden.
Only when a dialog is deleted it actually emits the destroyed signal.
So the question is where do you delete the dialog? Is it via explicit delete, a smart pointer or by assigning a parent?
For some reason my QDialog based class does not seem to have the 'closed' signal. How comes?
I 'delete' the dialog by clicking on the 'x' in the window decoration. Therefore I assume that I really want closed() instead of destroyed(). But how do I get it? Why isn't it there?
You didn't read carefully what I wrote.
There is no closed signal. When you click "X" the dialog only receives hideEvent and -does not emit any signals- emits accepted or rejected signal, depending on how it was closed.
Closing dialog (by clicking "X" or hitting alt+F4 or Esc) does not delete the dialog. Only hides it.
The destroyed signal is emitted when the dialog is deleted (not just hidden!).
How do you create and destroy the dialog? Can you paste the actual code fragment?
Ah, I'm sorry. I understand now what you mean.
This is how I create the dialog:
@_dialog = new CodeGeneratorDialog(_settingsData);@
And usually I don't close/delete the dialog by myself, I just wait until the user closes (hides) it. However, I still have this portion of a code in the object that creates the dialog:
And if it matters, my dialogs constructor looks like this:
@explicit CodeGeneratorDialog(const SettingsData* settingsData, QWidget *parent = 0);@
Ok, so the destroyed signal is emitted when you call "delete _dialog", not when you close it with "X".
You might want to connect to accepted or rejected signals to detect when it is closed, but the more usual way is to show a dialog via exec() and just check the returned value.
Btw. Identifier names starting with an underscore or double underscore are reserved for the implementation(compiler and STL) and you should not use them in your code.
The reason why I use show() instead of exec() is because the dialog is displaying the process of the object that creates the dialog. Therefore, the blocking exec() is not suitable to me.
using the accepted and rejected signals seem to work for now.
Thank you for the information about the underscore issue. I have been using this for almost two years now to identify/mark members of a class.
What is a more proper approach?
Edit: Yes, I am a C-Hacker that is slowly learning about the beauty of C++
There are many different ways to name your variables and I don't want to get into a holy war about it but these are the most common (I think):
- The hungarian notation: m_Dialog
- The semi-hungarian notation: mDialog
- The trailing underscore: dialog_
- IDE purist notation: dialog (and the IDE will show you if it's a member or not)
Okay, thank you very much for your time and suggestions.