View Full Version : need to filter Esc key for a dialog in a class
Askar
11th December 2009, 06:14
Dear friends,
i need to filter Esc key for a particular dialog in my project not for all dialogs, i tried with overriding
QDialog::KeyPressEvent(QKeyEvent *) but it applies for all dialogs in my project. i need help...
Thanks,
regards,
Askar
wagmare
11th December 2009, 06:21
install eventFilter for the QDialog and catch the key Qt::key
or use QAction for key and triggered to an empty slot()
Dialog:: eventFilter(QObject *ob, QEvent *e)
{
if(e->type() == Qt::Key_Escape){
printf("is it coming inside event..\n")
}
return QWidget::eventFilter(ob, e);
}
Askar
11th December 2009, 06:39
I cont understand how can i use ur code
wagmare
11th December 2009, 06:45
see eventFilter is a virtually protected property of QObject
see this link http://doc.trolltech.com/4.2/eventsandfilters.html
this protected function is used to filter the particular event on the widget ex: mouse press, keypress, mouse hover
using the event filter u can catch the key press of escape ..
this code
bool FilterObject::eventFilter(QObject *object, QEvent *event)
{
if (object == target && event->type() == QEvent::KeyPress) {
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
if (keyEvent->key() == Qt::Key_escape) {
// Special tab handling
return true;
} else
return false;
}
return fals
will filter the escape key input of the QDialog
Askar
11th December 2009, 07:09
I TRIED WITH THIS CODE
bool QDialog::eventFilter(QObject *object, QEvent *event)
{
if (event->type() == QEvent::KeyPress) {
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
if (keyEvent->key() == Qt::Key_Escape) {
keyEvent->ignore();
// Special tab handling
return true;
} else
return false;
}
return false;
}
BUT , THE CONDITION
if (event->type() == QEvent::KeyPress) FAILS FOR Esc KEYY PRESS\
wagmare
11th December 2009, 07:12
did u give installEventFilter(this) to the QDialog ... can u show us the code u written
Askar
11th December 2009, 07:29
sure here is my code
void CommonFunctions::progressbar(QString Dialogtext)
{
layout = new QVBoxLayout;
l = new QLabel( Dialogtext);
l->setWordWrap(true);
progress = new QProgressBar();
progress->setWindowModality(Qt::WindowModal);
progress->setRange(0,10);
layout->addWidget(l);
layout->addWidget(progress,Qt::AlignCenter);
win->setMaximumWidth(320);
win->setModal(true);
win->setLayout(layout);
win->setWindowTitle("title");
installEventFilter(win);
win->setWindowFlags(Qt::Dialog|Qt::WindowMaximizeButton Hint);
win->show();
}
bool QDialog::eventFilter(QObject *object, QEvent *event)
{
if (event->type() == QEvent::KeyPress) {
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
if (keyEvent->key() == Qt::Key_Escape) {
keyEvent->ignore();
return true;
} else
return false;
}
return false;
}
i had checked this code by setting a breakpoint in eventFilter(), while pressing Esc key debugger stops at break point and checks the condition in 22 line of the above code , but it fails & returns false
where im getting wrong can u help me....
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.