PDA

View Full Version : How to close (exit) a dialog properly



cooper
8th March 2011, 03:24
Hi everyone,

I need open a new dialog (form) from main form.


void MainScr::on_pushButton_2_clicked()
{
if (!cyclescrdialog)
cyclescrdialog = new cycleScrDialog(NULL);
cyclescrdialog->exec();
}


in the beginning of the new form, i initialize two leds, "label_led_1" and "label_led_2", to be green.


cycleScrDialog::cycleScrDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::cycleScrDialog)
{
ui->setupUi(this);

QPoint pos; // relocate window
pos.setX(0); // x
pos.setY(0); // y
move(pos);

picLedRed = QPixmap("/home/Pictures/ledred.png");
picLedGreen = QPixmap("/home/Pictures/ledgreen.png");

ui->label_led_1->setPixmap(picLedGreen);
ui->label_led_2->setPixmap(picLedGreen);
// ....
// ....
}

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

// exit button
void cycleScrDialog::on_pushButton_4_clicked()
{
close();
}


in the middle of program, these leds will be changed to be red.
after i click "exit" button to close this window (dialog), and re-open it, I found that these two leds are still red.
When i step through my program, it did not go through the beginning of cycleScrDialog. it means that the close() function does not close completely, all variables in cycleScrDialog has not been released from memory.

can anyone point me where i did wrong, and how to close a dialog properly?

Thanks in advance.

mcosta
8th March 2011, 07:56
it means that the close() function does not close completely, all variables in cycleScrDialog has not been released from memory.

You create you Dialog on heap


if (!cyclescrdialog)
cyclescrdialog = new cycleScrDialog(NULL);

so you don't delete it from memory.

Try with

cycleScrDialog cyclescrdialog (NULL);

In this way you create and delete it each time

cooper
8th March 2011, 20:40
Hi mcosta, thanks for your reply.
I have tried your code, but the program stop and show an error message:


The program has unexpectedly finished.


i am a newbie to Qt, when i learned how to create a new dialog (window), i studied some codes from this forum. Here is how i understand to create a new dialog, and i create all my new dialogs in the same way. can you point me is there anything wrong in my code, please?

in main frame (mainscr), i have a push button to open a second dialog (cyclescrdialog).
in mainscr.cpp


#include "mainscr.h"
#include "ui_mainscr.h"
// ******
#include "cyclescrdialog.h"
// ******
#include "qextserialport.h"
#include <qmessagebox.h>

extern cycleScrDialog *cyclescrdialog; //*****

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

void MainScr::on_pushButton_1_clicked()
{
if (!cyclescrdialog)
//cyclescrdialog = new cycleScrDialog(NULL);
cycleScrDialog cyclescrdialog(NULL); //if i try this code, program will stop
cyclescrdialog->exec();
}


in cyclescrdialog.cpp


#include "cyclescrdialog.h"
#include "ui_cyclescrdialog.h"
#include <qmessagebox.h>

cycleScrDialog *cyclescrdialog;

cycleScrDialog::cycleScrDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::cycleScrDialog)
{
ui->setupUi(this);

// remove window frame
Qt::WindowFlags flags = windowFlags();
if (flags.testFlag(Qt::FramelessWindowHint))
return;
flags |= Qt::FramelessWindowHint;
setWindowFlags(flags);
//...
//...
}

mcosta
9th March 2011, 09:45
Correct your cpp file as



#include "mainscr.h"
#include "ui_mainscr.h"
// ******
#include "cyclescrdialog.h"
// ******
#include "qextserialport.h"
#include <qmessagebox.h>

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

void MainScr::on_pushButton_1_clicked()
{
cycleScrDialog cyclescrdialog(NULL);
cyclescrdialog.exec();
}



When on_pushButton_1_clicked exits cyclescrdialog is destroyed

Gokulnathvc
9th March 2011, 09:49
Use

cyclescrdialog.show();

stampede
9th March 2011, 09:58
Use

cyclescrdialog.show();
Please don't post wrong answers - show() does not block, so you'll only see a quick flash of your dialog, as it'll be destroyed immediately after calling show() ( when exiting the method ). Using exec() blocks until dialog is closed by the user.

cooper
9th March 2011, 19:19
It works as what i need. Thanks for all you guys help. i really appreciate that :)