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
){
setAttribute(Qt::WA_AcceptTouchEvents);
}
bool ScrollArea
::event(QEvent* event
) {
switch (event->type()) {
{
QTouchEvent *touchEvent = static_cast<QTouchEvent *>(event);
QList<QTouchEvent::TouchPoint> touchPoints = touchEvent->touchPoints();
touchPoints.first().setLastPos(touchPoints.first().pos());
}
break;
{
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:
break;
}
}
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;
}
}
To copy to clipboard, switch view to plain text mode
Of course, I have create my class ToolButton to accept touchEvents:
this->setAttribute(Qt::WA_AcceptTouchEvent);
this->setAttribute(Qt::WA_AcceptTouchEvent);
To copy to clipboard, switch view to plain text mode
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.
Bookmarks