Results 1 to 7 of 7

Thread: QDialog and closeEvent

Hybrid View

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

    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.

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

    ricardo (12th July 2009)

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

    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.

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

    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 

  5. 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
  •  
Qt is a trademark of The Qt Company.