#ifndef MOUSEEVENTGENERATOR_H
#define MOUSEEVENTGENERATOR_H
#include <QtCore>
class QQuickView;
class MouseEventGenerator
: public QObject{
Q_OBJECT
public:
explicit MouseEventGenerator(QQuickView *viewer = 0);
public slots:
void generateMouseEvent();
void generateReleaseEventLeft();
void generateReleaseEventRight();
protected:
void postEvent(int x, int y, int gesture_code);
QQuickView* m_viewer;
};
#endif // MOUSEEVENTGENERATOR_H
#ifndef MOUSEEVENTGENERATOR_H
#define MOUSEEVENTGENERATOR_H
#include <QtCore>
class QQuickView;
class MouseEventGenerator : public QObject
{
Q_OBJECT
public:
explicit MouseEventGenerator(QQuickView *viewer = 0);
public slots:
void generateMouseEvent();
void generateReleaseEventLeft();
void generateReleaseEventRight();
protected:
void postEvent(int x, int y, int gesture_code);
QQuickView* m_viewer;
QString m_lastSide;
};
#endif // MOUSEEVENTGENERATOR_H
To copy to clipboard, switch view to plain text mode
#include "mouseeventgenerator.h"
#include <QtCore>
#include <QGuiApplication>
#include <QApplication>
#include <QtQuick/QQuickView>
MouseEventGenerator::MouseEventGenerator(QQuickView *viewer)
m_viewer(viewer),
m_lastSide("")
{
connect(timer, SIGNAL(timeout()), this, SLOT(generateMouseEvent()));
timer->start(3000);
}
void MouseEventGenerator::generateMouseEvent(){
if(m_lastSide == "right"){
postEvent(220, 220, 1);
QTimer::singleShot(300,
this,
SLOT(generateReleaseEventLeft
()));
m_lastSide = "left";
}else{
postEvent(420, 220, 1);
QTimer::singleShot(300,
this,
SLOT(generateReleaseEventRight
()));
m_lastSide = "right";
}
}
void MouseEventGenerator::generateReleaseEventLeft(){
postEvent(220, 220, 0);
}
void MouseEventGenerator::generateReleaseEventRight(){
postEvent(420, 220, 0);
}
void MouseEventGenerator::postEvent(int x, int y, int gesture_code){
if(gesture_code == 1){
Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);
QGuiApplication::postEvent(m_viewer, pressEvent);
}
else if(gesture_code == 0){
Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);
QGuiApplication::postEvent(m_viewer, releaseEvent);
}
}
#include "mouseeventgenerator.h"
#include <QtCore>
#include <QGuiApplication>
#include <QApplication>
#include <QtQuick/QQuickView>
MouseEventGenerator::MouseEventGenerator(QQuickView *viewer)
: QObject(),
m_viewer(viewer),
m_lastSide("")
{
QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(generateMouseEvent()));
timer->start(3000);
}
void MouseEventGenerator::generateMouseEvent(){
if(m_lastSide == "right"){
postEvent(220, 220, 1);
QTimer::singleShot(300, this, SLOT(generateReleaseEventLeft()));
m_lastSide = "left";
}else{
postEvent(420, 220, 1);
QTimer::singleShot(300, this, SLOT(generateReleaseEventRight()));
m_lastSide = "right";
}
}
void MouseEventGenerator::generateReleaseEventLeft(){
postEvent(220, 220, 0);
}
void MouseEventGenerator::generateReleaseEventRight(){
postEvent(420, 220, 0);
}
void MouseEventGenerator::postEvent(int x, int y, int gesture_code){
if(gesture_code == 1){
QMouseEvent *pressEvent = new QMouseEvent(QEvent::MouseButtonPress, QPoint(x, y),
Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);
QGuiApplication::postEvent(m_viewer, pressEvent);
}
else if(gesture_code == 0){
QMouseEvent *releaseEvent = new QMouseEvent(QEvent::MouseButtonRelease, QPoint(x,y),
Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);
QGuiApplication::postEvent(m_viewer, releaseEvent);
}
}
To copy to clipboard, switch view to plain text mode
import QtQuick 2.4
Item {
width: 640
height: 480
visible: true
Rectangle {
id: background
anchors.fill: parent
color: "black"
}
Rectangle {
id: rectLeft
x: 200
y: 200
width: 100; height: 100
color: "red"
MouseArea {
anchors.fill: parent
onPressed: {
parent.color = "green"
}
onReleased: {
parent.color = "red"
}
}
}
Rectangle {
id: rectRight
x: 400
y: 200
width: 100; height: 100
color: "red"
MouseArea {
anchors.fill: parent
onPressed: {
parent.color = "green"
}
onReleased: {
parent.color = "red"
}
}
}
}
import QtQuick 2.4
Item {
width: 640
height: 480
visible: true
Rectangle {
id: background
anchors.fill: parent
color: "black"
}
Rectangle {
id: rectLeft
x: 200
y: 200
width: 100; height: 100
color: "red"
MouseArea {
anchors.fill: parent
onPressed: {
parent.color = "green"
}
onReleased: {
parent.color = "red"
}
}
}
Rectangle {
id: rectRight
x: 400
y: 200
width: 100; height: 100
color: "red"
MouseArea {
anchors.fill: parent
onPressed: {
parent.color = "green"
}
onReleased: {
parent.color = "red"
}
}
}
}
To copy to clipboard, switch view to plain text mode
#include <QGuiApplication>
#include <QtQuick/QQuickView>
#include <QQmlContext>
#include "mouseeventgenerator.h"
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QQuickView view;
MouseEventGenerator *generator = new MouseEventGenerator(&view);
view.setResizeMode(QQuickView::SizeRootObjectToView);
view.
setSource(QUrl("qrc:/main.qml"));
view.show();
return app.exec();
}
#include <QGuiApplication>
#include <QtQuick/QQuickView>
#include <QQmlContext>
#include "mouseeventgenerator.h"
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QQuickView view;
MouseEventGenerator *generator = new MouseEventGenerator(&view);
view.setResizeMode(QQuickView::SizeRootObjectToView);
view.setSource(QUrl("qrc:/main.qml"));
view.show();
return app.exec();
}
To copy to clipboard, switch view to plain text mode
Bookmarks