Unsolved get in MyApplication::notify() again after return QApplication::notify()
-
Hi all
I want to distinguish between kayboard and scanner ( they both are keypress event). So I override the
notify()
then the code is as follow:bool CustomApplication::notify(QObject *target, QEvent *event) { if(event->type() == QEvent::KeyPress) { auto keyEvent = static_cast<QKeyEvent *>(event); if(m_receiceTime.isValid() && event->isAccepted()) { auto interval = m_receiceTime.msecsTo(QDateTime::currentDateTime()); m_receiceTime = QDateTime::currentDateTime(); qDebug("receive interval:%lld",interval); if(50 > interval) { qDebug()<<"fast key"<<keyEvent->text(); switch (keyEvent->key()) { ... } return true; // return QApplication::notify(target,event); } else { qDebug()<<"key"<<keyEvent->text(); m_receiceTime = QDateTime::currentDateTime(); return QApplication::notify(target,event); } } else { qDebug()<<"key"<<keyEvent->text(); m_receiceTime = QDateTime::currentDateTime(); return QApplication::notify(target,event); } } else { return QApplication::notify(target,event); } }
why one key event will be process twice?
Best regards
Mihan -
@Mihan said in get in MyApplication::notify() again after return QApplication::notify():
why one key event will be process twice?
I guess because you have two events:
- Key press
- Key release
See https://doc.qt.io/qt-5.15/qkeyevent.html
"Key events are sent to the widget with keyboard input focus when keys are pressed or released."
-
@jsulm said in get in MyApplication::notify() again after return QApplication::notify():
I guess because you have two events:
But I only process key press event.
Also I found the interval will between repetitive events.
e.g.
I press the 'A'
->key 'A' interval 254
->fast key 'A' interval 0 -
@Mihan said in get in MyApplication::notify() again after return QApplication::notify():
I want to distinguish between kayboard and scanner ( they both are keypress event). So I override the
notify()
then the code is as follow:What's a scanner?
Choosing to override QCoreApplication::notify() instead of using one of the other options mentioned in the documentation is an unusual choice.
@Mihan said in get in MyApplication::notify() again after return QApplication::notify():
But I only process key press event.
Also I found the interval will between repetitive events.
e.g.
I press the 'A'
->key 'A' interval 254
->fast key 'A' interval 0Have you examined the receiver of the event? Some events are delivered through a chain of objects, causing the same event to be passed through QCoreApplication::notify() multiple times. Again from the documentation:
For certain types of events (e.g. mouse and key events), the event will be propagated to the receiver's parent and so on up to the top-level object if the receiver is not interested in the event (i.e., it returns false).
-
@jeremy_k said in get in MyApplication::notify() again after return QApplication::notify():
What's a scanner?
It is used to scan the QR code or Bar code.
@jeremy_k said in get in MyApplication::notify() again after return QApplication::notify():
Have you examined the receiver of the event? Some events are delivered through a chain of objects, causing the same event to be passed through QCoreApplication::notify() multiple times.
Well, I think it's because of this. I'm checking it now. Thanks a lot.