[SOLVED] can't delete the dialog on dialog close
-
i have a small app that has a dialog and two buttons on the dialog. one button when clicked will write "test" to the dialogs widget's textedit, while the other button closes the dialog. the problem is when "test" is displayed in the textedit and i close and reopen the dialog, the word "test" is still displayed in the textedit. when i try to delete the dialog on close, the program crashes when i try to reopen the dialog. below is the code that i am using to close the dialog. i am trying to clear the word "test" from the textedit by trying to delete the dialog when it is closed.
mainwindow.cpp
@#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "dialog.h"MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
dia = new Dialog(this);
dia->setAttribute(Qt::WA_DeleteOnClose);
}MainWindow::~MainWindow()
{
delete ui;
}void MainWindow::on_pushButton_clicked()
{dia->show();
}@
dialog.cpp
@#include "dialog.h"
#include "ui_dialog.h"Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
}Dialog::~Dialog()
{
delete ui;
}void Dialog::on_pushButton_clicked()
{
ui->textEdit->append("test");
}void Dialog::on_pushButton_2_clicked()
{
this->close();
}@ -
[quote author="kalster" date="1323414648"]the problem is when "test" is displayed in the textedit and i close and reopen the dialog, the word "test" is still displayed in the textedit. when i try to delete the dialog on close, the program crashes when i try to reopen the dialog. below is the code that i am using to close the dialog. i am trying to clear the word "test" from the textedit by trying to delete the dialog when it is closed.
@[/quote]The word is still there because you haven't re-initialized the dialog and its widgets status. You have to clear all the widgets, if this is your aim, before you make the dialog visible.
And if you delete the dialog on close it is normal that the program crashes if you don't re-allocate the dialog. -
the problem seems to be that the Dialog::Dialog function is only read once. i need it to be read each time the dialog is opened. i guess i can use a showevent function.
-
Of course it is read once, it is a class constructor and it is read each time you create a new object. You have to modify your code something like this:
@
Dialog::clear(){ ui->textEdit->setText(""); }
@and
@void MainWindow::on_pushButton_clicked()
{
dia->clear();
dia->show();
}@if you want to use the same dialog over and over. Or with something like this:
@void MainWindow::on_pushButton_clicked()
{
dia = new Dialog( this );
dia->show();
}@if you want to use each time a new dialog. Of course in such case you have to remove other dialog creations in the constructor of your main window.
-
thank you fluca1978. that helped. i marked this topic as solved. :)
-
The design is massively wrong. You have a class member pointing to a Dialog - or not pointing to a Dialog but to a bunch of garbage bytes. It depends whether the Dialog is deleted.
If you click on the MainWindow's button a second time while the dialog is open, you do not show that dialog, but open a new one - I doubt that this is what you want.
So, either keep dia as member of your MainWindow, but do not destroy it on close in order to be able to reuse it on a second click while it is open, or remove the dia member - as you do not need the pointer anywhere else there's no reason to keep it in the class.
-
thank you Volker. i will have a look into that.