PDA

View Full Version : New Dialog Problem



hacquerqop
17th July 2018, 09:31
Why is this happening?
12902

//mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QtCore>
#include<QtGui>
#include<QMessageBox>
#include"secondwindow.h"

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}

MainWindow::~MainWindow()
{
delete ui;
}

void MainWindow::on_pushButton_clicked()
{
QMessageBox::information (this, "Title", "kaydedildi");
}

void MainWindow::on_pushButton_3_clicked() //When i clicked this button the problem occurs
{
msecond = new Secondwindow(this);
msecond->show();
}

hacquerqop
17th July 2018, 12:27
I found the solution.
for those one who are wondering



void MainWindow::on_pushButton_3_clicked()
{
msecond = new Secondwindow(this); //it should be like msecond = new Secondwindow();
msecond->show();

d_stranz
17th July 2018, 15:43
Your code has a memory leak. Each time the button_clicked slot is executed, you create a -new- Secondwindow instance. Unless you delete this later, it creates a memory leak.

The usual model for creating and displaying dialogs is to create them on the stack, use a call to exec() to show them as modal dialogs, respond to the return value from the dialog, and exit the slot:



void MainWindow::on_pushButton_3_clicked()
{
Secondwindow msecond(this);
if ( QDialog::Accepted == msecond.exec() )
{
// do something with the result
}
} // slot exits, second window gets destroyed automatically