How to properly clean-up a QWidget / manage a set of windows? - c++

Let's say I have 2 windows in my application, and two classes responsible for them:
class MainWindow: public QMainWindow and class SomeDialog: public QWidget.
In my Main Window I have a button. When it is clicked, I need to display the second window. I do it this way:
SomeDialog * dlg = new SomeDialog();
dlg.show();
Now, the user does something in the window, and closes it. At this point I want to get some data from that window, and then, I suppose, I will have to delete dlg. But how do I catch the event of that window being closed?
Or is there another way not to have a memory leak? Maybe It would be better to create an instance of each window on startup, and then just Show()/Hide() them?
How do I manage such a case?

It is advised to use show() / exec() and hide() instead of dynamically creating the dialog every time you want to show it. Also use QDialog instead of QWidget.
In the constructor of your main window create it and hide it
MainWindow::MainWindow()
{
// myDialog is class member. No need to delete it in the destructor
// since Qt will handle its deletion when its parent (MainWindow)
// gets destroyed.
myDialog = new SomeDialog(this);
myDialog->hide();
// connect the accepted signal with a slot that will update values in main window
// when the user presses the Ok button of the dialog
connect (myDialog, SIGNAL(accepted()), this, SLOT(myDialogAccepted()));
// remaining constructor code
}
In the slot connected to the buttons's clicked() event simply show it, and if necessary pass some data to the dialog
void myClickedSlot()
{
myDialog->setData(data);
myDialog->show();
}
void myDialogAccepted()
{
// Get values from the dialog when it closes
}

Subclass from QWidget and reimplement
virtual void QWidget::closeEvent ( QCloseEvent * event )
http://doc.qt.io/qt-4.8/qwidget.html#closeEvent
Also it looks like the widget you want to show is a dialog. So consider using QDialog or it's subclasses. QDialog has useful signals you can connect to:
void accepted ()
void finished ( int result )
void rejected ()

I think you are looking for the Qt::WA_DeleteOnClose window flag: http://doc.qt.io/archives/qt-4.7/qt.html#WidgetAttribute-enum
QDialog *dialog = new QDialog(parent);
dialog->setAttribute(Qt::WA_DeleteOnClose)
// set content, do whatever...
dialog->open();
// safely forget about it, it will be destroyed either when parent is gone or when the user closes it.

Related

Qt - How to handle memory management for dialogs?

I am running into the following issue:
Users presses "Ctrl+N" which goes into function MainWindow::newAction()
In MainWindow::newAction(), create a QDialog dlg(centralWidget()) and call dlg.exec()
While dlg is open, users pressed "Ctrl+N" again
The result is that dlg never gets deleted (it will only get deleted once centralWidget() gets deleted).
The call stack is something like this:
MainWindow::newAction ()
...
MainWindow::newAction()
I am wondering how to handle this case. I want all of the local dialog variables from the first call to newAction() to be deleted by the time we go into the function newAction() again.
You also can try something like this:
void MainWindow::newAction() {
const auto dialog = new MyDialog(centralWidget());
// When user will close the dialog it will clear itself from memory
connect(dialog, &QDialog::finished, [dialog]() {
dialog->deleteLater();
});
dialog->exec();
}
However, a good move would be to stop user from summoning more QDialogs than a single one, given that this one is a modal dialog(might be a good idea to keep this dialog pointer as a class member and check is it on screen already before calling exec() on it.
If i understood the question right, you want one dialog to be opened and want to delete it before a new dialog request comes in?
If that's the case you can do following:
In MainWindow.h declare QDialog *dlg = nullptr
In your MainWindow.cpp newAction() function you can do following:
void newAction()
{
if(dlg != nullptr)
{
dlg->close();
dlg->deleteLater();
//or
//dlg->destroy(); // this will immediately free memory
}
dlg = new QDialog(centralWidget());
...
//dlg->exec(); // This will automatically make QDialog modal.
dlg->show(); // This will not make a QDialog modal.
}
I hope this will help. Remember QDialogs when displayed with exec() they automatically behave as Modal window. show() will make it non-modal.

How to close parent UI window when child UI window is open in QT

I have multiple UI windows in my QT project. When a new UI window opens, the previous UI window must be closed, that is, at every point of time only one UI window must be open. How can this be done?
I did that before and i suggest you to not close(delete) UI.
just hide it and when you need it show it again.
check this code:
when user click to see second UI:
void MainApp::on_btnSettings_clicked()
{
this->hide();
settingsManager = new SettingsManager(); // put this line in constructor
settingsManager->show();
}
on second UI on closing form(or back button) emit a signal:
void SettingsManager::closeEvent(QCloseEvent *event)
{
emit settingsBackToMainApp();
}
on main hide second class and show main:
void MainApp::settingsBackToMainApp()
{
settingsManager->hide();
this->show();
}
connect signal to slot:
connect(settingsManager,&SettingsManager::settingsBackToMainApp,this,&MainApp::settingsBackToMainApp); // put this line in constructor

Qt: button - going back from "help.cpp" to "mainwindow.cpp"

I'm new at Qt. I've created small application and I created second page help.cpp. On MainWindow.cpp I have a button, that switches to help.cpp page.
Function which switches to "help" page:
void MainWindow::on_box1button_clicked()
{
helpwindow = new help(this);
helpwindow->show();
}
This code works properly.
On the "help" page I've got a QButton, which will switch back to mainwindow.cpp. How Can I code that button to actually make this action?
If your intention by "switching" is hiding one window and showing another one, so you can simply pass a reference of the main window to your help window and there when you want to switch back, you can hide/close itself and show the main window.
MainWindow (this code is fine)
helpwindow = new help(this);
helpwindow->show();
HelpWindow
When you want to switch back to the main window, you can do this:
// Hide the HelpWindow itself
// or this->close()
this->hide()
// Show the MainWindow (i.e. the parent window)
QWidget *parent = this->parentWidget();
parent->show();
Since you are creating a new help(this); on mainwindow it is better to close the help window
Use
this->close();

How to create dialog without blocking main form?

For now I can do:
void MainWindow::on_actionPATH_triggered() {
std::unique_ptr<QDialog> win(new QDialog());
win->exec();
}
Should I use async / run in separate threadto avoid blocking main window or is there way to subscribe to close even and delete / free object there?
You can use just show()
void MainWindow::on_actionPATH_triggered() {
QDialog* win = new QDialog();
//needed connect
win->setAttribute(Qt::WA_DeleteOnClose);//we don't want memory leak
win->show();
}
and use
win->setModal(false);//but it is default option, you don't need to change it
From doc:
By default, this property is false and show() pops up the dialog as
modeless. Setting his property to true is equivalent to setting
QWidget::windowModality to Qt::ApplicationModal. exec() ignores the
value of this property and always pops up the dialog as modal.
Qt::WA_DeleteOnClose will delete your dialog, when user close it.
You can also set parent to dialog:
QDialog* win = new QDialog(this);
In this case win will be delete with your mainWindow.
Info about Qt parent child relationship
And you don't need here separate thread.

Qt show modal dialog (.ui) on menu item click

I want to make a simple 'About' modal dialog, called from Help->About application menu. I've created a modal dialog window with QT Creator (.ui file).
What code should be in menu 'About' slot?
Now I have this code, but it shows up a new modal dialog (not based on my about.ui):
void MainWindow::on_actionAbout_triggered()
{
about = new QDialog(0,0);
about->show();
}
Thanks!
You need to setup the dialog with the UI you from your .ui file. The Qt uic compiler generates a header file from your .ui file which you need to include in your code. Assumed that your .ui file is called about.ui, and the Dialog is named About, then uiccreates the file ui_about.h, containing a class Ui_About. There are different approaches to setup your UI, at simplest you can do
#include "ui_about.h"
...
void MainWindow::on_actionAbout_triggered()
{
about = new QDialog(0,0);
Ui_About aboutUi;
aboutUi.setupUi(about);
about->show();
}
A better approach is to use inheritance, since it encapsulates your dialogs better, so that you can implement any functionality specific to the particular dialog within the sub class:
AboutDialog.h:
#include <QDialog>
#include "ui_about.h"
class AboutDialog : public QDialog, public Ui::About {
Q_OBJECT
public:
AboutDialog( QWidget * parent = 0);
};
AboutDialog.cpp:
AboutDialog::AboutDialog( QWidget * parent) : QDialog(parent) {
setupUi(this);
// perform additional setup here ...
}
Usage:
#include "AboutDialog.h"
...
void MainWindow::on_actionAbout_triggered() {
about = new AboutDialog(this);
about->show();
}
In any case, the important code is to call the setupUi() method.
BTW: Your dialog in the code above is non-modal. To show a modal dialog, either set the windowModality flag of your dialog to Qt::ApplicationModal or use exec() instead of show().
For modal dialogs, you should use exec() method of QDialogs.
about = new QDialog(0, 0);
// The method does not return until user closes it.
about->exec();
// In this point, the dialog is closed.
Docs say:
The most common way to display a modal dialog is to call its exec() function. When the user closes the dialog, exec() will provide a useful return value.
Alternative way: You don't need a modal dialog. Let the dialog show modeless and connect its accepted() and rejected() signals to appropriate slots. Then you can put all your code in the accept slot instead of putting them right after show(). So, using this way, you wouldn't actually need a modal dialog.