QTouchEvent / Mouse event interaction



  • I have a class derived from QGraphicsObject. I'd like users to be able to interact with this object with a mouse or a touchscreen ( if one is installed ). In some cases I need to toggle a state when either the mouse down or TouchBegin event is detected. The problem I'm having is that when the user touches the screen I get both a QEvent::TouchBegin and a mousePressEvent. Since my code needs to toggle the state of a variable in either of these cases I end up toggling twice. I've called event->accept() when I get any QEvent::Touch* events thinking that might block it from calling mousePressEvent but that doesn't appear to be the case. Is there a way to stop the mouse events from being called if I handle it through the touch events? I could set some internal state and block mouse events while handling touch events but that seems pretty sketchy.

    Here's my sample code which displays the problem. I'm running on Win7 64 if that matters.

    @#include <QtGui>

    class Test : public QGraphicsObject
    {
    virtual QRectF boundingRect() const
    {
    return QRectF(0,0,800,800);
    }
    void mousePressEvent ( QGraphicsSceneMouseEvent * event )
    {
    qDebug() << "Mouse button press";
    }
    void mouseMoveEvent ( QGraphicsSceneMouseEvent * event )
    {
    qDebug() << "Mouse move";
    }
    virtual void paint(QPainter painter, const QStyleOptionGraphicsItem option, QWidget widget = 0)
    {
    painter->setBackground(QColor(255,0,0));
    painter->fillRect(boundingRect(), QBrush(QColor(255,0,0)));
    painter->setPen(QColor(0,0,0));
    }
    bool sceneEvent(QEvent
    event)
    {
    switch(event->type())
    {
    case QEvent::TouchBegin:
    case QEvent::TouchEnd:
    case QEvent::TouchUpdate:
    {
    event->accept();
    QTouchEvent
    touchEvent = static_cast<QTouchEvent
    >(event);
    if( touchEvent)
    {
    for( int ix = 0; ix < touchEvent->touchPoints().size(); ++ix)
    {
    const QTouchEvent::TouchPoint& p1 = touchEvent->touchPoints().at(ix);
    qDebug() << "touch " << event << " is at " << p1.scenePos().x() << ", " << p1.scenePos().y() << " : " << p1.id();
    }
    }
    return true;
    }
    break;
    }
    return QGraphicsObject::sceneEvent(event);
    }

    public:
    Test()
    {
    this->setAcceptTouchEvents(true);
    }
    };

    int main(int argc, char argv[])
    {
    QApplication a(argc, argv);
    QGraphicsScene
    scene = new QGraphicsScene(0, 0, 800, 800);
    scene->addItem(new Test());
    QGraphicsView view(scene);
    view.show();
    return a.exec();
    }
    @



  • Ok, the post may be outdated and you have solved this issue for sure, but when i read your post, i rememdered one thing i read in the "documenation of QTouchEvent":http://doc.qt.nokia.com/latest/qtouchevent.html, which sounds like the solution for your problem.

    Mouse Events and the Primary Touch Point

    QTouchEvent delivery is independent from that of QMouseEvent. On some windowing systems, mouse events are also sent for the primary touch point. This means it is possible for your widget to receive both QTouchEvent and QMouseEvent for the same user interaction point. You can use the QTouchEvent::TouchPoint::isPrimary() function to identify the primary touch point.

    Note that on some systems, it is possible to receive touch events without a primary touch point. All this means is that there will be no mouse event generated for the touch points in the QTouchEvent.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.