Tap Gesture Example on Desktop
Jump to navigation
Jump to search
Just yesterday a team mate asked me how to fake a gesture event on a desktop, say by clicking on a button, and I quickly wrote this example using Qt Designer form, a push button when clicked sends a tap gesture. Comments welcome :)
PS. Just remember: Gesture objects are not constructed directly by developers. They are created by the QGestureRecognizer object that is registered with the application; see QGestureRecognizer::registerRecognizer().
<br />#include "mainwindow.h&quot;<br />#include "ui_mainwindow.h&quot;<br />#include <QDebug&gt;<br />#include <QTapGesture&gt;<br />#include <QGestureEvent&gt;
MainWindow::MainWindow(QWidget *parent) :<br /> QMainWindow(parent),<br /> ui(new Ui::MainWindow)<br />{<br /> ui->setupUi(this);<br /> connect(ui->pushButton, SIGNAL (clicked()), this, SLOT (gest()));<br /> grabGesture(Qt::TapGesture);<br />}
MainWindow::~MainWindow()<br />{<br /> ungrabGesture(Qt::TapGesture);<br /> delete ui;<br />}
void MainWindow::gest()<br />{<br /> QTapGesture '''tapGes = new QTapGesture(this);<br /> tapGes->setPosition(QPointF(5,5));<br /> QList&lt;QGesture'''> tapGesTureList;<br /> tapGesTureList.append(tapGes);<br /> QGestureEvent event(tapGesTureList);<br /> QCoreApplication::sendEvent(this, &event);<br />}
bool MainWindow::event(QEvent *event)<br />{<br /> if (event->type() == QEvent::Gesture)<br /> {<br /> QGestureEvent '''gestevent = static_cast&lt;QGestureEvent'''>(event);<br /> if (QGesture *gest = gestevent->gesture(Qt::TapGesture))<br /> {<br /> QTapGesture '''tapgest = static_cast&lt;QTapGesture'''>(gest);<br /> qDebug() << "tap gesture got at: " << tapgest->position();<br /> }<br /> }<br /> return true;<br />}<br />