Problem with button debounce in Qt4.8 embedded
-
I'm writing an application for the BeagleBone Black, currently running on an Angstrom Linux Distribution and Qt Embedded 4.8 and a 5.7" LCD screen with a resistive touch screen.
My problem is that when I press any of my on screen buttons I frequently get multiple presses sent. I'm using the pressed signal in my button handling code. Is there any way to adjust the button press debounce timing so the display is less sensitive?
-
Hi,
If you can't do it at the driver level, you can use a QTimer that you restart when you get something from your button. Doing so you can add easily a minimal delay that will allow to only trigger your Qt signal once after your button finishes to bounce.
Hope it helps
-
I was hoping to not have to implement debounce code in every button handler, at this point I've probably got 50 buttons.
-
Well, why not make a class that does that ?
-
If there isn't code in Qt that handles input bounce then there should be code in the underlying driver that tries to handle it and I'll have to do more research to find out where to look.
-
There is no code in Qt for that since it's not its job to handle driver level stuff
-
You can implement debouncing at application level, using one function.
Install an event filter on QApplication and spurious mouse events can be blocked.I ran into this same problem and could not find an answer so I solved myself. See below.
Basically, if the touch screen was pressed to quickly after release, treat it as a bounce.@class MyApp : public QApplication
{
public:
MyApp(int& argc, char **argv, Type type) :
QApplication(argc, argv, type)
{
installEventFilter(this);
}private:
/*!
Intercept events at the topmost level.
@param o The object watched (in this case, it is MyApp and can be ignored.
@param e The event.
@return true to consume the event.
*/
bool eventFilter(QObject *o, QEvent *e)
{
static QTime t;
static bool isBounced = false;
int msec;
switch(e->type())
{
case QEvent::MouseButtonPress:
case QEvent::MouseButtonDblClick:
msec = t.elapsed();
isBounced = (msec < 20); // Experiment with this number
if (isBounced)
return true;
break;
case QEvent::MouseButtonRelease:
t.restart();
if (isBounced)
return true;
break;
default:
break;
}
// Let Qt handle events that were not consumed here.
return QApplication::eventFilter(o, e);
}int main(int argc, char **argv)
{
MyApp a(argc, argv, QApplication::GuiServer);// your code return a.exec();
}
@ -
Thanks for the suggestion Ken. I had put a debounce timer in a common class, but I have to call it from every button handler. The event filter looks much better.