Tap Gesture Example on Desktop: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 3: Line 3:
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().'''
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().'''


<code><br />#include &quot;mainwindow.h&amp;quot;<br />#include &quot;ui_mainwindow.h&amp;quot;<br />#include &lt;QDebug&amp;gt;<br />#include &lt;QTapGesture&amp;gt;<br />#include &lt;QGestureEvent&amp;gt;
<code>
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QTapGesture>
#include <QGestureEvent>


MainWindow::MainWindow(QWidget *parent) :<br /> QMainWindow(parent),<br /> ui(new Ui::MainWindow)<br />{<br /> ui-&gt;setupUi(this);<br /> connect(ui-&gt;pushButton, SIGNAL (clicked()), this, SLOT (gest()));<br /> grabGesture(Qt::TapGesture);<br />}
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(ui->pushButton, SIGNAL (clicked()), this, SLOT (gest()));
grabGesture(Qt::TapGesture);
}


MainWindow::~MainWindow()<br />{<br /> ungrabGesture(Qt::TapGesture);<br /> delete ui;<br />}
MainWindow::~MainWindow()
{
ungrabGesture(Qt::TapGesture);
delete ui;
}


void MainWindow::gest()<br />{<br /> QTapGesture '''tapGes = new QTapGesture(this);<br /> tapGes-&gt;setPosition(QPointF(5,5));<br /> QList&amp;lt;QGesture'''&gt; tapGesTureList;<br /> tapGesTureList.append(tapGes);<br /> QGestureEvent event(tapGesTureList);<br /> QCoreApplication::sendEvent(this, &amp;event);<br />}
void MainWindow::gest()
{
QTapGesture '''tapGes = new QTapGesture(this);
tapGes->setPosition(QPointF(5,5));
QList<QGesture'''> tapGesTureList;
tapGesTureList.append(tapGes);
QGestureEvent event(tapGesTureList);
QCoreApplication::sendEvent(this, &amp;event);
}


bool MainWindow::event(QEvent *event)<br />{<br /> if (event-&gt;type() == QEvent::Gesture)<br /> {<br /> QGestureEvent '''gestevent = static_cast&amp;lt;QGestureEvent'''&gt;(event);<br /> if (QGesture *gest = gestevent-&gt;gesture(Qt::TapGesture))<br /> {<br /> QTapGesture '''tapgest = static_cast&amp;lt;QTapGesture'''&gt;(gest);<br /> qDebug() &lt;&lt; &quot;tap gesture got at: &quot; &lt;&lt; tapgest-&gt;position();<br /> }<br /> }<br /> return true;<br />}<br /></code>
bool MainWindow::event(QEvent *event)
{
if (event->type() == QEvent::Gesture)
{
QGestureEvent '''gestevent = static_cast<QGestureEvent'''>(event);
if (QGesture *gest = gestevent->gesture(Qt::TapGesture))
{
QTapGesture '''tapgest = static_cast<QTapGesture'''>(gest);
qDebug() << "tap gesture got at: " << tapgest->position();
}
}
return true;
}
</code>

Revision as of 10:15, 25 February 2015

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().

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QTapGesture>
#include <QGestureEvent>

MainWindow::MainWindow(QWidget *parent) :
 QMainWindow(parent),
 ui(new Ui::MainWindow)
{
 ui->setupUi(this);
 connect(ui->pushButton, SIGNAL (clicked()), this, SLOT (gest()));
 grabGesture(Qt::TapGesture);
}

MainWindow::~MainWindow()
{
 ungrabGesture(Qt::TapGesture);
 delete ui;
}

void MainWindow::gest()
{
 QTapGesture '''tapGes = new QTapGesture(this);
 tapGes->setPosition(QPointF(5,5));
 QList<QGesture'''> tapGesTureList;
 tapGesTureList.append(tapGes);
 QGestureEvent event(tapGesTureList);
 QCoreApplication::sendEvent(this, &amp;event);
}

bool MainWindow::event(QEvent *event)
{
 if (event->type() == QEvent::Gesture)
 {
 QGestureEvent '''gestevent = static_cast<QGestureEvent'''>(event);
 if (QGesture *gest = gestevent->gesture(Qt::TapGesture))
 {
 QTapGesture '''tapgest = static_cast<QTapGesture'''>(gest);
 qDebug() << "tap gesture got at: " << tapgest->position();
 }
 }
 return true;
}