PDA

View Full Version : QDialog problem



waynew
27th June 2010, 23:41
Yeah, I know this is a simple C++ problem, but I cannot find an answer.
Not wanting a user to be able to instantiate a non-modal dialog more than once, I tried the following code (just like on p. 64 of C++ Gui Programming with Qt 4)


if (!help) {
help = new HelpViewer(this); // header has HelpViewer *help;
help->show();
}

The problem is that even though a debug before the if statement shows that help is false, the code in the if statement never gets executed.
So, what am I doing wrong here? Or is there a better method to accomplish this?

SixDegrees
27th June 2010, 23:58
I'll note that, if help is non-zero, the dialog will never be shown, because the show() statement is only executed inside the if{} statement.

Also, it isn't clear that help is zero to begin with. Is it being set to zero explicitly in the class constructor? Not all compilers set variables to default values.

ahmdsd_ostora
28th June 2010, 07:16
you should initialize the help variable before using.

help = 0;
is a must before checking its value, or u will have strange behavior

waynew
28th June 2010, 13:10
Ok, you guys have helped - I'm almost there now.
Moved the HelpViewer *help; from the header to the source, so now help is 0 the first time I try to instantiate the help dialog object.
So far, so good. But, how do I get help to return to 0 when the helpviewer window closes?
If I use help->setAttribute(Qt::WA_DeleteOnClose, true); then the pointer is gone, so that doesn't work.

gutiory
28th June 2010, 13:22
Hello.
I've one question to you. does HelpViewer heritate from QDialog?.
If it's true, you can reimplement closeEvent in your HelpViewer class.


void HelpViewer::closeEvent(QCloseEvent *event)
{

// You can do what you want here

}

I hope that helps.

ahmdsd_ostora
28th June 2010, 13:30
u must listen to a signal for your "help" instance on closing, if it's QDialog u can get finished() signal and set help =0 int it
if it's a QWidget u can get closeEvent() and set help = 0 int it

Lesiok
28th June 2010, 14:07
Just use QPointer in place of ordinary pointer.

waynew
28th June 2010, 23:53
Thanks to all who replied, I appreciate it.
Problem is now solved with a signal from the QWidget class when the window closes, setting help = 0 in the slot in the MainWindow.
Works perfectly.