Results 1 to 7 of 7

Thread: QDialog and closeEvent

  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

    Smile QDialog and closeEvent

    Hi dudes!

    Is there anyway to execute some code after the user press ESC key? I override closeEvent but it does not work (but it works if I press X window button)

    I read on docs if ESC key is pressed, closeEvent can not be ignored, but I just want to execute some code.


    Thanks a lot.

  2. #2
    Join Date
    Jul 2009
    Posts
    74
    Thanks
    2
    Thanked 6 Times in 6 Posts

    Default Re: QDialog and closeEvent

    override reject()

    if you close a dialog with ESC, X window button or alt+f4 goes to reject()

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

    ricardo (11th July 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: QDialog and closeEvent

    Quote Originally Posted by javimoya View Post
    override reject()

    if you close a dialog with ESC, X window button or alt+f4 goes to reject()
    Thanks.

    By the way, should I do something like this:

    Qt Code:
    1. void CDialogLevelProperties::reject() {
    2. // some code before canceling this dialog
    3. // bla bla bla
    4. QDialog::reject();
    5. }
    To copy to clipboard, switch view to plain text mode 

  5. #4
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QDialog and closeEvent

    ya ... this should work..

  6. #5
    Join Date
    Feb 2008
    Posts
    153
    Thanks
    40
    Thanked 8 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QDialog and closeEvent

    What I did was override the keyPressEvent:

    Qt Code:
    1. void myClass::keyPressEvent(QKeyEvent* event) {
    2. if (event->key() != Qt::Key_Escape) {
    3. QDialog::keyPressEvent(event);
    4. }
    5. else {
    6. close();
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 

    Then this fires the closeEvent() which you can then work with there. But perhaps the above solution is easier.

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

    ricardo (12th July 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: QDialog and closeEvent

    Quote Originally Posted by codeslicer View Post
    What I did was override the keyPressEvent:

    Qt Code:
    1. void myClass::keyPressEvent(QKeyEvent* event) {
    2. if (event->key() != Qt::Key_Escape) {
    3. QDialog::keyPressEvent(event);
    4. }
    5. else {
    6. close();
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 

    Then this fires the closeEvent() which you can then work with there. But perhaps the above solution is easier.
    Actually that was my former solution. That code does not work when user press X button. javimoya solution is better. Anyway, thanks.

  9. #7
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QDialog and closeEvent

    Quote Originally Posted by ricardo View Post
    Actually that was my former solution. That code does not work when user press X button. javimoya solution is better. Anyway, thanks.
    you have to accept the event and this will work

    Qt Code:
    1. void myClass::keyPressEvent(QKeyEvent* event)
    2. {
    3. if (event->key() != Qt::Key_Escape)
    4. {
    5. QDialog::keyPressEvent(event);
    6. }
    7. else
    8. {
    9. event->accept();//Bye Bye event
    10. close();
    11. }
    12. }
    To copy to clipboard, switch view to plain text mode 

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

    ricardo (13th July 2009)

Similar Threads

  1. Disable Close button (X) of a QDialog
    By BrainB0ne in forum Qt Programming
    Replies: 29
    Last Post: 8th December 2009, 17:01
  2. closeEvent help needed
    By sgmurphy19 in forum Qt Programming
    Replies: 1
    Last Post: 5th November 2007, 17:51

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.