user1581608
user1581608

Reputation:

Simulating touch event from QT Application

I have a QT Application which receives X&Y coordinates and I need to simulate touch events (touch or long touch) using these coordinates.

I read in QT Reference documentation that QTouchEventSequence class can be used to simulate touch events. I am pretty new to QT and I have no idea how can I use it in an application. Could anybody help me on this.

Upvotes: 2

Views: 5869

Answers (1)

graphite
graphite

Reputation: 2958

So you want to use QtTest module. It has a good tutorial.

For quick start here are a quick example for QTouchEventSequence:

// file test.cc
#include <QtGui>
#include <QtTest>


// This widget will receive touch events
class MyWidget: public QWidget
{
    Q_OBJECT
public:
    MyWidget(QWidget* parent = NULL)
      : QWidget(parent)
    {
        // With out this flag widget shouldn't receive any TouchEvent.
        setAttribute(Qt::WA_AcceptTouchEvents);
    }

protected:
    bool event(QEvent* e)
    {
        switch(e->type())
        {
            case QEvent::TouchBegin:
            case QEvent::TouchUpdate:
            case QEvent::TouchEnd:
                emit newTouchEvent(); // We will catch this in our test
                return true;
            default:
                return QWidget::event(e);
        }
    }

signals:
    void newTouchEvent();
};


// Here out test
class TouchTest: public QObject
{
    Q_OBJECT

private slots:
    void simpleTest();
};

void TouchTest::simpleTest()
{
    MyWidget widget;
    widget.show();
    QSignalSpy spy(&widget, SIGNAL(newTouchEvent()));

    // Simulate touch event
    QTest::touchEvent(&widget).press(0, QPoint(10, 10));

    QCOMPARE(spy.count(), 1);  // Did we called our signal. Yes. Cool!
}

QTEST_MAIN(TouchTest)
#include "test.moc"

You sould run moc, qmake and make to run it:

qmake -project "CONFIG += qtestlib"
qmake
moc test.cc > test.moc
make
./test

Upvotes: 1

Related Questions