Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QScrollArea and TouchEvent



  • 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);
    QListQTouchEvent::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.


Log in to reply