Tap Gesture Example on Desktop: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
 
m (Wieland moved page TapGestureExampleOnDesktop to Tap Gesture Example on Desktop: Better title)
 
(5 intermediate revisions by 3 users not shown)
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>
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QTapGesture>
#include <QGestureEvent>


* [[:Category:Learning|Learning]]
MainWindow::MainWindow(QWidget *parent) :
** [[:Category:Learning::Demos and Examples|Demos_and_Examples]]
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, &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;
}
</code>

Latest revision as of 15:28, 24 March 2016

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, &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;
}