How to open MainWindow with signal
-
I want to open MainWindow with button clicked signal, but the MainWindow close right after open it, why this happening?
Dialog dialog; dialog.show(); QObject::connect( &dialog, &Dialog::onSetClicked, [](const QString &port, const QString &config, const QString &model) { MainWindow demo(port, config, model); demo.show(); }); -
I want to open MainWindow with button clicked signal, but the MainWindow close right after open it, why this happening?
Dialog dialog; dialog.show(); QObject::connect( &dialog, &Dialog::onSetClicked, [](const QString &port, const QString &config, const QString &model) { MainWindow demo(port, config, model); demo.show(); });@isan said in How to open MainWindow with signal:
QObject::connect(
&dialog, &Dialog::onSetClicked,
[](const QString &port, const QString &config, const QString &model) {
MainWindow demo(port, config, model); --> local variable !
demo.show();
}); -
@isan said in How to open MainWindow with signal:
QObject::connect(
&dialog, &Dialog::onSetClicked,
[](const QString &port, const QString &config, const QString &model) {
MainWindow demo(port, config, model); --> local variable !
demo.show();
}); -
Hi,
You create demo in your lambda. It will be destroyed at the end of the function.
-
@isan How can I fix it?
MainWindow* demo=new MainWindow(port, config, model);Don't forget to add this in the MainWindow constructor:
setAttribute(Qt::WA_DeleteOnClose);otherwise you will have a memory leak.