Results 1 to 8 of 8

Thread: How can I avoid a QDialog close?

  1. #1
    Join Date
    Apr 2009
    Posts
    132
    Thanks
    67
    Thanked 6 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Question How can I avoid a QDialog close?

    Hi!

    If I have QMainWindow I can rewrite
    void CEditor::closeEvent(QCloseEvent *event) {
    and return event->ignore(); to avoid closing.

    How can I do something similar if I have a QDialog? (user press X button or hit ESC)

    Thanks.

  2. #2
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How can I avoid a QDialog close?

    closeEvent() is for all widgets, not just main window..so it should work with dialog too..if it doesnt, try setting window modality for dialog to true..that way it will become a TLW(top level widget), and closeEvent() would most certainly come to dialog

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

    ricardo (11th May 2009)

  4. #3
    Join Date
    Apr 2009
    Posts
    132
    Thanks
    67
    Thanked 6 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How can I avoid a QDialog close?

    It seems if I rewrite
    void CSequencesDialog::closeEvent(QCloseEvent *event) {
    works, but if I try to write
    event->accept();
    Or
    event->ignore();

    [/code]
    1>.\CSequencesDialog.cpp(475) : error C2027: use of undefined type 'QCloseEvent'
    1> c:\qt\2009.01\qt\include\qtgui\../../src/gui/kernel/qwidget.h(83) : see declaration of 'QCloseEvent'
    1>.\CSequencesDialog.cpp(475) : error C2227: left of '->accept' must point to class/struct/union/generic type
    1>.\CSequencesDialog.cpp(477) : error C2027: use of undefined type 'QCloseEvent'
    1> c:\qt\2009.01\qt\include\qtgui\../../src/gui/kernel/qwidget.h(83) : see declaration of 'QCloseEvent'
    1>.\CSequencesDialog.cpp(477) : error C2227: left of '->ignore' must point to class/struct/union/generic type
    1>Build log was saved at "file://c:\Documents and Settings\Ricardo\Escritorio\iPhone\Kokochiko\Edito r\Editor\Debug\BuildLog.htm"
    1>Editor - 4 error(s), 0 warning(s)
    [code]

    Any idea?

  5. #4
    Join Date
    Apr 2009
    Posts
    132
    Thanks
    67
    Thanked 6 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How can I avoid a QDialog close?

    Bad news. I read this on Qt docs:

    "If the user presses the Esc key in a dialog, QDialog::reject() will be called. This will cause the window to close: The close event cannot be ignored."

    So, is that impossible?

    Thanks.

  6. #5
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How can I avoid a QDialog close?

    May be you can try overriding QWidget::keyPressEvent or QWidget::keyReleaseEventand compare something like -
    Qt Code:
    1. if(event->key() == Qy::Key_Esc)
    2. event->ignore(); // or do nothing, dont call base call function for this case
    To copy to clipboard, switch view to plain text mode 
    Last edited by aamer4yu; 11th May 2009 at 05:33.

  7. The following user says thank you to aamer4yu for this useful post:

    ricardo (11th May 2009)

  8. #6
    Join Date
    Apr 2009
    Posts
    132
    Thanks
    67
    Thanked 6 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How can I avoid a QDialog close?

    I have a similar problem if I use keyPressEvent. It seems parameter is not recognized.

    Qt Code:
    1. >CSequencesDialog.cpp
    2. 1>.\CSequencesDialog.cpp(486) : error C2027: use of undefined type 'QKeyEvent'
    3. 1> c:\qt\2009.01\qt\include\qtgui\../../src/gui/kernel/qwidget.h(78) : see declaration of 'QKeyEvent'
    4. 1>.\CSequencesDialog.cpp(486) : error C2227: left of '->key' must point to class/struct/union/generic type
    5. 1>.\CSequencesDialog.cpp(486) : error C2653: 'Qy' : is not a class or namespace name
    6. 1>.\CSequencesDialog.cpp(486) : error C2065: 'Key_Esc' : undeclared identifier
    7. 1>.\CSequencesDialog.cpp(486) : error C2027: use of undefined type 'QKeyEvent'
    8. 1> c:\qt\2009.01\qt\include\qtgui\../../src/gui/kernel/qwidget.h(78) : see declaration of 'QKeyEvent'
    9. 1>.\CSequencesDialog.cpp(486) : error C2227: left of '->ignore' must point to class/struct/union/generic type
    10. 1>CEditor.cpp
    11. 1>Generating Code...
    To copy to clipboard, switch view to plain text mode 

  9. #7
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How can I avoid a QDialog close?

    you forgot to include
    Qt Code:
    1. #include <QKeyEvent>
    To copy to clipboard, switch view to plain text mode 
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  10. The following user says thank you to spirit for this useful post:

    ricardo (11th May 2009)

  11. #8
    Join Date
    Apr 2009
    Posts
    132
    Thanks
    67
    Thanked 6 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How can I avoid a QDialog close?

    Thanks a lot, it worked.

Similar Threads

  1. QDialog with Close, Maximize and Context Help Buttons
    By mclark in forum Qt Programming
    Replies: 7
    Last Post: 1st May 2020, 16:53
  2. Disable Close button (X) of a QDialog
    By BrainB0ne in forum Qt Programming
    Replies: 29
    Last Post: 8th December 2009, 17:01
  3. QDialog not showing close button on Mac
    By manojmka in forum Qt Programming
    Replies: 2
    Last Post: 17th September 2008, 12:56
  4. Why when I close QDialog my main application gets killed?
    By alex chpenst in forum Qt Programming
    Replies: 2
    Last Post: 10th July 2008, 22:57
  5. Replies: 1
    Last Post: 7th July 2007, 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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.