PDA

View Full Version : keyPressEvent problem



amulya
26th July 2006, 08:28
hi,

i m trying to capture the key press event. Now, this works for all other keys except the Tab key. What could be the problem?? Here is the code



Image_check::Image_check(QWidget *parent, Qt::WFlags flags)
: QWidget(parent, flags)
{
ui.setupUi(this);
ui.pushButton->setFocus();
/* ui.pushButton->setFocusPolicy(Qt::StrongFocus );
ui.pushButton_2->setFocusPolicy(Qt::StrongFocus );*/

}
void Image_check::keyPressEvent(QKeyEvent *e)
{
if(e->key() == Qt::Key_Tab)
{
if(sender() == ui.pushButton)
{
QMessageBox::information(this, "Application name",
"first"
"The factory default will be used instead.");
}
else
{
QMessageBox::information(this, "Application name",
"second"
"The factory default will be used instead.");
}
QWidget::keyPressEvent(e);
}
else
QWidget::keyPressEvent(e);
}

I need to capture the Tab key..and that only is not working
Please tell ur suggestions on this..Thanx

Amulya

wysota
26th July 2006, 09:12
You can't catch the tab key in keyPressEvent. Override event() instead.


bool MyClass:event( QEvent *evt ) {
if ( evt->type() == QEvent::KeyPress ) {
QKeyEvent *ke = (QKeyEvent *)evt;
if ( ke->key() == Key_Tab ) {
// special tab handling here
ke->accept();
return TRUE;
}
}
return QWidget::event( evt );
}

anafor2004
22nd January 2008, 12:51
" QKeyEvent *ke = (QKeyEvent *)evt; "

I couldn't do this conversion. IDE always appers this message "unused variable 'ke' ".I think there is some thing wrong here.

deneme.cpp:27: warning: unused variable 'ke'
deneme.cpp:28: error: `ke' undeclared (first use this function)

aamer4yu
22nd January 2008, 13:06
There must be some typing mistake,,, can u post the code part which is giving error ??

anafor2004
22nd January 2008, 13:16
okay i solve this problem.