Results 1 to 8 of 8

Thread: QDialog problem

  1. #1
    Join Date
    May 2009
    Location
    USA
    Posts
    300
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    82
    Thanked 11 Times in 11 Posts

    Default QDialog problem

    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)
    Qt Code:
    1. if (!help) {
    2. help = new HelpViewer(this); // header has HelpViewer *help;
    3. help->show();
    4. }
    To copy to clipboard, switch view to plain text mode 
    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?

  2. #2
    Join Date
    Apr 2010
    Posts
    769
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11
    Thanks
    1
    Thanked 94 Times in 86 Posts

    Default Re: QDialog problem

    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.

  3. The following user says thank you to SixDegrees for this useful post:

    waynew (28th June 2010)

  4. #3
    Join Date
    Jun 2010
    Posts
    86
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    10
    Thanked 6 Times in 4 Posts

    Default Re: QDialog problem

    you should initialize the help variable before using.

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

  5. #4
    Join Date
    May 2009
    Location
    USA
    Posts
    300
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    82
    Thanked 11 Times in 11 Posts

    Default Re: QDialog problem

    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.

  6. #5
    Join Date
    Oct 2009
    Posts
    28
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    4
    Thanked 2 Times in 2 Posts

    Default Re: QDialog problem

    Hello.
    I've one question to you. does HelpViewer heritate from QDialog?.
    If it's true, you can reimplement closeEvent in your HelpViewer class.

    Qt Code:
    1. void HelpViewer::closeEvent(QCloseEvent *event)
    2. {
    3.  
    4. // You can do what you want here
    5.  
    6. }
    To copy to clipboard, switch view to plain text mode 

    I hope that helps.

  7. #6
    Join Date
    Jun 2010
    Posts
    86
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    10
    Thanked 6 Times in 4 Posts

    Default Re: QDialog problem

    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

  8. The following user says thank you to ahmdsd_ostora for this useful post:

    waynew (28th June 2010)

  9. #7
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,540
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanked 284 Times in 279 Posts

    Default Re: QDialog problem

    Just use QPointer in place of ordinary pointer.

  10. #8
    Join Date
    May 2009
    Location
    USA
    Posts
    300
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    82
    Thanked 11 Times in 11 Posts

    Default Re: QDialog problem

    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.

Similar Threads

  1. QDialog problem
    By bostero22 in forum Newbie
    Replies: 6
    Last Post: 27th December 2015, 18:26
  2. QDialog problem with exec()
    By nomadscarecrow in forum Qt Programming
    Replies: 3
    Last Post: 23rd April 2010, 18:40
  3. Problem with QDialog
    By sudhansu in forum Qt Programming
    Replies: 0
    Last Post: 8th March 2010, 10:11
  4. Problem with QDialog
    By sudheer168 in forum Qt Programming
    Replies: 2
    Last Post: 2nd December 2009, 12:26
  5. Problem with QDialog class
    By sudheer168 in forum Qt Programming
    Replies: 4
    Last Post: 23rd December 2008, 09:03

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.