Tap Gesture Example on Desktop: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
 
No edit summary
Line 1: Line 1:
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 <span class="smiley">:)</span>
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().'''
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().'''


===Categories:===
<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;


* [[:Category:Learning|Learning]]
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 />}
** [[:Category:Learning::Demos and Examples|Demos_and_Examples]]
 
MainWindow::~MainWindow()<br />{<br /> ungrabGesture(Qt::TapGesture);<br /> delete ui;<br />}
 
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 />}
 
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>

Revision as of 09:55, 24 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().

<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;

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()<br />{<br /> ungrabGesture(Qt::TapGesture);<br /> delete ui;<br />}

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 />}

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 />