PDA

View Full Version : QScrollArea and TouchEvent



Francknos
18th June 2014, 10:59
Hello,

My problem today is about tactil event on QScrollArea.
My view has:

One ScrollArea which contain some ToolButton

I create my class ScrollArea like this:



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

bool ScrollArea::event(QEvent* event)
{
switch (event->type()) {
case QEvent::TouchBegin:
{
QTouchEvent *touchEvent = static_cast<QTouchEvent *>(event);
QList<QTouchEvent::TouchPoint> touchPoints = touchEvent->touchPoints();
touchPoints.first().setLastPos(touchPoints.first() .pos());
}

break;
case QEvent::TouchUpdate:
case QEvent::TouchEnd:
{
QTouchEvent *touchEvent = static_cast<QTouchEvent *>(event);

QList<QTouchEvent::TouchPoint> touchPoints = touchEvent->touchPoints();
const QTouchEvent::TouchPoint &touchPoint0 = touchPoints.first();
if(touchPoints.count() == 1 && !touchPoint0.lastPos().isNull() )
{
// determine scale factor
QPointF delta = touchPoint0.pos() - touchPoint0.lastPos() ;
if(touchPoint0.lastPos().x() > touchPoint0.pos().x())//old position is qpoint it remembers last position
this->horizontalScrollBar()->setValue(this->horizontalScrollBar()->value() - delta.x());

if(touchPoint0.lastPos().x() < touchPoint0.pos().x())
this->horizontalScrollBar()->setValue(this->horizontalScrollBar()->value() - delta.x());

if(touchPoint0.lastPos().y() > touchPoint0.pos().y())
this->verticalScrollBar()->setValue(this->verticalScrollBar()->value() - delta.y());

if(touchPoint0.lastPos().y() < touchPoint0.pos().y())
this->verticalScrollBar()->setValue(this->verticalScrollBar()->value() - delta.y());

return true;
}

}break;

default:
return QWidget::event(event);
break;
}
}

Of course, I have create my class ToolButton to accept touchEvents:


this->setAttribute(Qt::WA_AcceptTouchEvent);

The scroll works fine if the start of my touch is on the background of ScrollArea. If my first touch begin on ToolButton, the event is not sent to my parent (here ScrollArea).

Someone can help me?

Thanks.

StrikeByte
19th June 2014, 11:24
Do you want to handle the touch event in the toolbutton? if not then disable the accept touch event, then the event should be propagated to the parent widget (scroll area).
The touch events will go up the list of parents until one of then accepts the event or uses an eventfilter that consumes the event. Be sure to set the parent on the toolbutton!

Francknos
20th June 2014, 16:37
Yes I want handle the event in ToolButon so the event no sent to parent ...
But an handle just for click on the ToolButon.