Results 1 to 20 of 20

Thread: How to disable ALT + F4

  1. #1
    Join Date
    Jul 2009
    Posts
    40
    Thanks
    7

    Default How to disable ALT + F4

    Hello

    I want to prevent the user to prematurely closes my dialog box. So I disabled the close button in the system menu using setWindowFlags. But still ALT + F4 is working , and the user can close the dialog with this key combination. So , how to disable the ALT+ F4 to prevent closing, if this is possible at all ?

    Thank you all

  2. #2
    Join Date
    Dec 2008
    Location
    TaganrogNativelandChehov,Russia
    Posts
    64
    Thanks
    1
    Thanked 8 Times in 7 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to disable ALT + F4

    Last edited by kwisp; 15th October 2009 at 14:31.
    east or west home is best

  3. #3
    Join Date
    Jul 2009
    Posts
    40
    Thanks
    7

    Default Re: How to disable ALT + F4

    Yes I can ignore the event to prevent closing. But how can I ignore the event only if ALT +F4 was pressed ? i.e I don't want to ignore the event if the user closes the dialog with the X ( Close ) button. Or this is to much to ask ?

  4. #4
    Join Date
    Jul 2008
    Location
    Germany
    Posts
    512
    Thanks
    11
    Thanked 76 Times in 74 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to disable ALT + F4

    Hi, you can try to catch the keyPressEvent for ALT + F4, but I'm not sure if there is a keyPressEvent for this combination or if it just creates the closeEvent.

    Ginsengelf

  5. #5
    Join Date
    Jul 2009
    Posts
    40
    Thanks
    7

    Default Re: How to disable ALT + F4

    I was looking to find the same , but can't find it.
    I am sure there is must some way to say if ALT +F4 was pressed

  6. #6
    Join Date
    Jul 2009
    Posts
    40
    Thanks
    7

    Default Re: How to disable ALT + F4

    I try the following , but not working :

    Qt Code:
    1. bool MyDlg::event(QEvent *event)
    2. {
    3. QKeyEvent* myKeyEvent=(QKeyEvent*)event;
    4. if(myKeyEvent->matches(QKeySequence::Close))
    5. return true;
    6. return QDialog::event(event);
    7. }
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Dec 2008
    Location
    TaganrogNativelandChehov,Russia
    Posts
    64
    Thanks
    1
    Thanked 8 Times in 7 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to disable ALT + F4

    see my "crutch" example you can simplify it
    Qt Code:
    1. #include <QApplication>
    2. #include <QUrl>
    3. #include <QVariant>
    4. #include <QString>
    5. #include <QtGlobal>
    6. #include <QDebug>
    7. #include <QWidget>
    8. #include <QKeyEvent>
    9. #include <QCloseEvent>
    10. #include <QMouseEvent>
    11. #include <QEvent>
    12.  
    13. class CloseHandler:public QObject {
    14. private:
    15. bool flag;
    16. protected:
    17. bool eventFilter(QObject* obj, QEvent* event)
    18. {
    19. if(obj->isWidgetType()) {
    20. switch(event->type()) {
    21. case QEvent::Close: {
    22. qDebug()<<__func__<<"Close";
    23. if(flag){
    24. event->ignore();
    25. return true;
    26. }
    27. //flag = false;
    28. break;
    29. }
    30. case QEvent::KeyPress: {
    31. if(((QKeyEvent*)event)->key() == Qt::Key_F4 || (((QKeyEvent*)event)->modifiers() == Qt::AltModifier)) {
    32. flag = true;
    33. }
    34. break;
    35. }
    36. case QEvent::MouseButtonPress://??
    37. case QEvent::MouseButtonRelease://??
    38. case QEvent::MouseMove://??
    39. case QEvent::Leave://!!
    40. flag = false;
    41. default: break;
    42. }
    43. }
    44. return QObject::eventFilter(obj,event);
    45. }
    46. };
    47.  
    48. int main(int a ,char** b )
    49. {
    50. QApplication app(a,b);
    51. CloseHandler h;
    52. w.installEventFilter(&h);
    53. w.show();
    54. return app.exec();
    55. }
    To copy to clipboard, switch view to plain text mode 
    east or west home is best

  8. The following 2 users say thank you to kwisp for this useful post:

    elizabeth.h1 (16th October 2009), Olumide (12th December 2013)

  9. #8
    Join Date
    May 2009
    Location
    Gorontalo
    Posts
    200
    Thanks
    20
    Thanked 5 Times in 5 Posts
    Qt products
    Platforms
    Unix/X11 Windows

    Question Re: How to disable ALT + F4

    How to disable escape key too ? I modified your code, but not work

    Qt Code:
    1. case QEvent::KeyPress:
    2. {
    3. if (((QKeyEvent*)event)->key() == Qt::Key_F4 || (((QKeyEvent*)event)->modifiers() == Qt::AltModifier))
    4. {
    5. flag = true;
    6. }
    7.  
    8. //this is my code
    9. if (((QKeyEvent*)event)->key() == Qt::Key_Escape) {
    10. flag =true;
    11. }
    12.  
    13. break;
    14. }
    To copy to clipboard, switch view to plain text mode 

  10. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to disable ALT + F4

    What is the point of disallowing the user to close the window using a keyboard shortcut and at the same time allow him to do it using the mouse? Both actions come from the user, why differentiate them?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. #10
    Join Date
    Dec 2008
    Location
    TaganrogNativelandChehov,Russia
    Posts
    64
    Thanks
    1
    Thanked 8 Times in 7 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to disable ALT + F4

    wirasto,
    in what widget are you whant to ate escape?
    east or west home is best

  12. #11
    Join Date
    May 2009
    Location
    Gorontalo
    Posts
    200
    Thanks
    20
    Thanked 5 Times in 5 Posts
    Qt products
    Platforms
    Unix/X11 Windows

    Question Re: How to disable ALT + F4

    If me press Escape button then dialog will close. How to prevent it ? I must installEventFilter to every widget ?

  13. #12
    Join Date
    Dec 2008
    Location
    TaganrogNativelandChehov,Russia
    Posts
    64
    Thanks
    1
    Thanked 8 Times in 7 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to disable ALT + F4

    you mast read abut QDialog first of all.

    after that i can suggest you to reimplement void QDialog::reject() [protected]
    like this
    Qt Code:
    1. ...
    2. void reject()
    3. {
    4. //nothing :)
    5. }
    6. ...
    To copy to clipboard, switch view to plain text mode 
    east or west home is best

  14. #13
    Join Date
    Jul 2009
    Posts
    40
    Thanks
    7

    Default Re: How to disable ALT + F4

    "If me press Escape button then dialog will close. How to prevent it ? I must installEventFilter to every widget ?"

    You can make new class , say MyQtDialog then reimplement keyPressEvent there like
    this :
    Qt Code:
    1. class MyQtDialog:public QDialog
    2. {
    3. Q_OBJECT;
    4. .....
    5.  
    6. void keyPressEvent(QKeyEvent* e)
    7. {
    8. if(e->key() == Qt::Key_Escape)
    9. return;
    10.  
    11. }
    12. }
    To copy to clipboard, switch view to plain text mode 

    Then use this class as base class for all dialog classes.

  15. #14
    Join Date
    May 2009
    Location
    Gorontalo
    Posts
    200
    Thanks
    20
    Thanked 5 Times in 5 Posts
    Qt products
    Platforms
    Unix/X11 Windows

    Cool Re: How to disable ALT + F4

    I'm not create new class for QDialog, but I reimplement keyPressEvent in protected. And works now


    kwisp,
    Without installEventFilter and reimplement reject(), all close function can't work anymore This is amazing

  16. #15
    Join Date
    Dec 2008
    Location
    TaganrogNativelandChehov,Russia
    Posts
    64
    Thanks
    1
    Thanked 8 Times in 7 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to disable ALT + F4

    wirasto,
    i do not understand you
    east or west home is best

  17. #16
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to disable ALT + F4

    I think he means keyPressEvent is virtual protected, and he has overrode its function. It's a standard thing to do in Qt land.

  18. #17
    Join Date
    Dec 2008
    Location
    TaganrogNativelandChehov,Russia
    Posts
    64
    Thanks
    1
    Thanked 8 Times in 7 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to disable ALT + F4

    i use eventFilter and installEventFilter becouse at begining this topick had very special question -- how to disable ALT+F4(Close window by X-button in top right coner and do not close by ALT+F4) i dont know realy why author whant this... he asked - i answer.

    How to disable Key_Escape in QDialog? -- It is another question. Key_Escape is special key for QDialog. It is meaning -- QDialog::reject(). And i intend that if we whant not close dialog by reject we must write subclass(class MDialog: public QDialog) and reimplement virtual slot: void reject().
    But this disable button "Cancel" and X-button.

    If we whant to close dialog by "Cancel"-button and X-button, and do not close by Escape(i realy do not know what for) -- It is necessary to brain scatter.
    east or west home is best

  19. #18
    Join Date
    Feb 2010
    Posts
    20
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Default Re: How to disable ALT + F4

    hi,

    is this piece of code worked for any of you guys?

    //

    .....
    case QEvent::KeyPress: {

    if(((QKeyEvent*)event)->key() == Qt::Key_F4 || (((QKeyEvent*)event)->modifiers() == Qt::AltModifier)) {

    ...........

    //



    I think "||" should be "&&".
    Any way thats not important, what important is is this key sequence creating the close event that can be caught?
    What i feel, its not taking the control to the code put with in such condition and the widget is being closed directly?

    Could any one give a clear picture of catching the event created by Alt + F4?

    Edit: Its creates a closeEvent(). but can we catch the key press like above?
    Last edited by netmat; 22nd June 2010 at 15:00.

  20. #19
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to disable ALT + F4

    Alt+F4 is a special key sequence (like Ctrl-Alt-Del), I don't think you can trap it without writing a driver. You can however (as stated above) trap the resulting close event.

  21. #20
    Join Date
    Jul 2009
    Location
    Enschede, Netherlands
    Posts
    462
    Thanked 69 Times in 67 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to disable ALT + F4

    It is a good idea in general to keep standard key combinations working in your program. It helps making your program easier to use by decreasing the learning curve.
    Horse sense is the thing that keeps horses from betting on people. --W.C. Fields

    Ask Smart Questions

Similar Threads

  1. How to disable keyboard
    By garry_3peace in forum Newbie
    Replies: 9
    Last Post: 27th October 2009, 04:17
  2. how to disable OnItem drops in a QListView?
    By rgl in forum Qt Programming
    Replies: 2
    Last Post: 31st January 2009, 15:26
  3. How to disable NextButton in QWizard ?
    By litroncn in forum Qt Programming
    Replies: 3
    Last Post: 27th May 2008, 07:05

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.