PDA

View Full Version : Capturing Touch Screen events in MainWindow



lightydo
20th February 2013, 13:21
Hi,

i have already a Qt application implemented and running in embedded linux. I am trying to implement a sleep mode where the system cpu frequency goes into powersave mode and when the touch screen is touched it will bump up the CPU back to performance. you will note that the application works just fine right now so touch screen wise no problem.
the problem i have is trying to capture QEvent::TouchBegin , QEvent::TouchUpdate, and QEvent::TouchEnd.

I read several posts and tried the following. None is successful, i.e. it would print the first"I'm in..." message but apparently I am not getting the Touch evens i am waiting for.

1. Override MainWindow event handler (MainWindow inherits QMainWindow)

in constructor:
setAttribute(Qt::WA_AcceptTouchEvents);


bool MainWindow::event(QEvent *event)
{
printf("\r\n I'm in...");fflush(stdout);

switch (event->type()) {
case QEvent::TouchBegin:
case QEvent::TouchUpdate:
case QEvent::TouchEnd:
case QEvent::MouseButtonPress:
printf("\r\n Touched!!!");fflush(stdout);
break;

default:
break;
}
return QMainWindow::event(event); // I want to also get the button press events.
}


2. Create a TouchScreenFilter widget and install an eventfilter for the main window.

main()
{
QApplication a (argc, argv);
MainWindow w;
TouchScreenFilter filter;

w.installEventFilter(&filter);
}



#include <QWidget>
#include <QEvent>

class TouchScreenFilter : public QWidget
{
Q_OBJECT
public:
explicit TouchScreenFilter(QWidget *parent = 0);

signals:

public slots:

protected:
bool eventFilter(QObject *obj, QEvent *ev);
};

TouchScreenFilter::TouchScreenFilter(QWidget *parent) : QWidget(parent)
{
setAttribute(Qt::WA_AcceptTouchEvents);
}

bool TouchScreenFilter::eventFilter(QObject *obj, QEvent *event)
{
printf("\r\n I'm in...");fflush(stdout);

switch (event->type()) {
case QEvent::TouchBegin:
case QEvent::TouchUpdate:
case QEvent::TouchEnd:
printf("\r\n Touched!!!");fflush(stdout);
break;

default:
break;
}
return QWidget::eventFilter(obj, event);
}


3. Override MainWindow eventFilter;

protected:
bool eventFilter(QObject*, QEvent*);


bool MainWindow::eventFilter(QObject *obj, QEvent *event)
{
printf("\r\n I'm in...");fflush(stdout);

switch (event->type()) {
case QEvent::TouchBegin:
case QEvent::TouchUpdate:
case QEvent::TouchEnd:
printf("\r\n Touched!!!");fflush(stdout);
break;

default:
break;
}
return QMainWindow::eventFilter(obj, event);
}


Any Idea what is wrong? why am I not getting the actual Touch events??

thanks,
Daniel.

zenick
30th September 2013, 16:23
I have the same problem, Daniel you solved the problem?