PDA

View Full Version : QEvent problem



batileon
4th May 2009, 02:57
Is there any possible reason that an ui will ignore keyboard event?
The case is like this: I am having an ui form, and installed the eventFilter to the form. So if there's key pressed from keyboard, it should run the event filter code. It works well originally, however after some actions, e.g. opening new ui form etc. I found that the event can no longer be detected!

How can I know if it is the form that missed the event signal, or there's actually no signal sent from the keyboard?

wysota
4th May 2009, 07:55
Is there any possible reason that an ui will ignore keyboard event?
No, I don't think so.


The case is like this: I am having an ui form, and installed the eventFilter to the form. So if there's key pressed from keyboard, it should run the event filter code. It works well originally, however after some actions, e.g. opening new ui form etc. I found that the event can no longer be detected!
Can we see the code?


How can I know if it is the form that missed the event signal, or there's actually no signal sent from the keyboard?

You can install an event filter on the application object. All events from within the application will be passing through it.

batileon
4th May 2009, 09:11
It is actually a very big program, I try to post part of the code here


void Login::setFilter()
{
LockLbl->installEventFilter( this );

Key1->installEventFilter( this );
Key2->installEventFilter( this );
Key3->installEventFilter( this );
Key4->installEventFilter( this );
Key5->installEventFilter( this );
Key6->installEventFilter( this );
Key7->installEventFilter( this );
Key8->installEventFilter( this );
Key9->installEventFilter( this );
Key0->installEventFilter( this );
Hyphen->installEventFilter( this );
BackSpace->installEventFilter( this );
Exit->installEventFilter( this );
Reset->installEventFilter( this );
Enter->installEventFilter( this );

ShowMode->installEventFilter( this );
ShowOutlet->installEventFilter( this );
LoginEdt->installEventFilter( this );
PasswdEdt->installEventFilter( this );
this->installEventFilter( this );
}


bool Login::eventFilter( QObject* object, QEvent *event )
{
#if (QT_VERSION >= 0x030300)
if ( event->type() == QEvent::LanguageChange )
{
return true;
}
#endif
Logger l;
if( event->type() == QEvent::MouseButtonPress ||
event->type() == QEvent::MouseButtonDblClick )
{
if( widgetIgnore )
return true;
else
{
if( strcmp( object->name(), "LockLbl" )==0 )
{
QApplication::beep();
lockEmployee();
setActiveWindow();
Reset_clicked();
}
}
}
if( event->type() == QEvent::FocusIn && object->isA( "QLineEdit" ) )
{
l.debug( "Login", "eventFilter FocusIn", focus + (string)" : " + object->name() );
if( focus == 'L' && strcmp( object->name(), "PasswdEdt" )==0 )
{
LoginEdt->setFocus();
return true;
}
if( focus == 'P' && strcmp( object->name(), "LoginEdt" )==0 )
{
PasswdEdt->setFocus();
return true;
}
}
if (event->type() ==QEvent::KeyPress && object->isA( "QLineEdit" ))
{
QKeyEvent *ke = (QKeyEvent *) event;
//ut.showMsg( this, "2", tr( "KeyPressed" ) );
// Do nothing if not pressing Enter in touch screen mode
cout<<"TETING LOGIN"<<endl;
if( Gstation.getInputMode() == 1 && ke->key() != Qt::Key_Enter && ke->key() != Qt::Key_Return )
{
return false;
}

keyClassification( ke );
return true;
}
else if( event->type() ==QEvent::WindowActivate )
{
if ( serialon == "yes" && slogin == "yes" )
{
if ( !proc->isRunning() )
{
cout << "init Serial" << endl;
if ( !proc->start() )
{
// error handling
cerr << "~~~~~~~ `` ERROR `` ~~~~~~~~" << endl;
}
}
}
this->setEnabled(true);
}
else if( event->type() ==QEvent::WindowDeactivate )
{
if ( !this->isHoldScreen )
{
shutSerial();
}
}
return false;
}

So, there's a line of cout in the event filter loop for QEvent::KeyPress, and the object I am filtering is really a QLineEdit. However, the cout never appear when the case I mentioned occurs, i.e. It works well at the beginning, and fail after calling some more ui.

wysota
4th May 2009, 12:23
Hmmm.... what are those event filters suppose to do? Can't use use signals and slots instead? Installing event filters on 20 objects might not be the best design decision, I'm almost sure there is a simpler way to do what you are trying to do.