PDA

View Full Version : How to disable ALT + F4



elizabeth.h1
15th October 2009, 15:04
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

kwisp
15th October 2009, 15:11
see
QWidget::closeEvent ;
QCloseEvent ;
QEvent ;

elizabeth.h1
15th October 2009, 15:25
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 ?

Ginsengelf
15th October 2009, 16:04
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

elizabeth.h1
15th October 2009, 16:23
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

elizabeth.h1
15th October 2009, 16:40
I try the following , but not working :



bool MyDlg::event(QEvent *event)
{
QKeyEvent* myKeyEvent=(QKeyEvent*)event;
if(myKeyEvent->matches(QKeySequence::Close))
return true;
return QDialog::event(event);
}

kwisp
16th October 2009, 09:56
see my "crutch" example you can simplify it :)


#include <QApplication>
#include <QUrl>
#include <QVariant>
#include <QString>
#include <QtGlobal>
#include <QDebug>
#include <QWidget>
#include <QKeyEvent>
#include <QCloseEvent>
#include <QMouseEvent>
#include <QEvent>

class CloseHandler:public QObject {
private:
bool flag;
protected:
bool eventFilter(QObject* obj, QEvent* event)
{
if(obj->isWidgetType()) {
switch(event->type()) {
case QEvent::Close: {
qDebug()<<__func__<<"Close";
if(flag){
event->ignore();
return true;
}
//flag = false;
break;
}
case QEvent::KeyPress: {
if(((QKeyEvent*)event)->key() == Qt::Key_F4 || (((QKeyEvent*)event)->modifiers() == Qt::AltModifier)) {
flag = true;
}
break;
}
case QEvent::MouseButtonPress://??
case QEvent::MouseButtonRelease://??
case QEvent::MouseMove://??
case QEvent::Leave://!!
flag = false;
default: break;
}
}
return QObject::eventFilter(obj,event);
}
};

int main(int a ,char** b )
{
QApplication app(a,b);
QWidget w;
CloseHandler h;
w.installEventFilter(&h);
w.show();
return app.exec();
}

wirasto
17th October 2009, 11:16
How to disable escape key too ? I modified your code, but not work



case QEvent::KeyPress:
{
if (((QKeyEvent*)event)->key() == Qt::Key_F4 || (((QKeyEvent*)event)->modifiers() == Qt::AltModifier))
{
flag = true;
}

//this is my code
if (((QKeyEvent*)event)->key() == Qt::Key_Escape) {
flag =true;
}

break;
}

wysota
17th October 2009, 11:39
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?

kwisp
17th October 2009, 12:03
wirasto,
in what widget are you whant to ate escape?

wirasto
17th October 2009, 14:59
If me press Escape button then dialog will close. How to prevent it ? I must installEventFilter to every widget ?

kwisp
18th October 2009, 12:26
you mast read abut QDialog first of all.
:)
after that i can suggest you to reimplement void QDialog::reject() [protected]
like this


...
void reject()
{
//nothing :)
}
...

elizabeth.h1
18th October 2009, 14:57
"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 :


class MyQtDialog:public QDialog
{
Q_OBJECT;
.....

void keyPressEvent(QKeyEvent* e)
{
if(e->key() == Qt::Key_Escape)
return;

}
}


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

wirasto
18th October 2009, 15:31
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 :)

kwisp
18th October 2009, 22:07
wirasto,
i do not understand you :(

squidge
18th October 2009, 22:42
I think he means keyPressEvent is virtual protected, and he has overrode its function. It's a standard thing to do in Qt land.

kwisp
19th October 2009, 10:18
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. :)

netmat
22nd June 2010, 15:53
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?

squidge
22nd June 2010, 18:25
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.

franz
22nd June 2010, 23:05
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.