Skip to content

Instantly share code, notes, and snippets.

@oKcerG
Last active March 27, 2017 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oKcerG/d380a8cc6df376a298a47368044244ac to your computer and use it in GitHub Desktop.
Save oKcerG/d380a8cc6df376a298a47368044244ac to your computer and use it in GitHub Desktop.
MouseProxy
import QtQuick 2.6
import QtQuick.Layouts 1.0
import Qt.labs.controls 1.0
import QtQuick.Layouts 1.3
import MouseProxy 0.1
ApplicationWindow {
visible: true
width: 640
height: 480
Row {
spacing: 10
padding: 10
Button {
width: 200
text: "Yo"
MouseProxy.target: loButton
}
Button {
id: loButton
text: "Lo"
}
}
}
#include "mouseproxyattached.h"
#include <QMouseEvent>
#include <QHoverEvent>
#include <QDebug>
#include <QToolTip>
MouseProxyAttached::MouseProxyAttached(QObject* parent) : QObject(parent)
{
if (parent)
{
parent->installEventFilter(this);
m_item = qobject_cast<QQuickItem*>(parent);
m_item->setAcceptHoverEvents(true);
}
}
MouseProxyAttached* MouseProxyAttached::qmlAttachedProperties(QObject *object)
{
return new MouseProxyAttached(object);
}
QQuickItem* MouseProxyAttached::target() const
{
return m_target;
}
void MouseProxyAttached::setTarget(QQuickItem *target)
{
if (m_target == target)
return;
m_target = target;
emit targetChanged(target);
}
bool MouseProxyAttached::eventFilter(QObject *obj, QEvent *event)
{
//() << event;
if (QMouseEvent* mouseEvent = dynamic_cast<QMouseEvent*>(event))
{
QPointF sourceLocalPos = mouseEvent->localPos();
QPointF mappedLocalPos(sourceLocalPos.x()*m_target->width()/m_item->width(),
sourceLocalPos.y()*m_target->height()/m_item->height());
QMouseEvent* mappedEvent = new QMouseEvent(
event->type(),
mappedLocalPos,
m_item->mapToItem(m_target, mouseEvent->windowPos()),
m_item->mapToItem(m_target, mouseEvent->screenPos()),
mouseEvent->button(),
mouseEvent->buttons(),
mouseEvent->modifiers(),
mouseEvent->source());
QCoreApplication::postEvent(m_target, mappedEvent);
}
if (event->type() == QEvent::HoverEnter)
{
QHoverEvent* hoverEvent = static_cast<QHoverEvent*>(event);
//QToolTip::showText(m_item->mapToGlobal(hoverEvent->pos()).toPoint(), "Tooltip\n:)");
}
if (event->type() == QEvent::HoverLeave)
{
//QToolTip::hideText();
}
return false;
}
void registerMouseProxyAttachedType()
{
qmlRegisterUncreatableType<MouseProxyAttached>("MouseProxy", 0, 1, "MouseProxy", "");
}
Q_COREAPP_STARTUP_FUNCTION(registerMouseProxyAttachedType)
#ifndef MOUSEPROXYATTACHED_H
#define MOUSEPROXYATTACHED_H
#include <QObject>
#include <QQuickItem>
#include <QtQml>
class MouseProxyAttached : public QObject
{
Q_OBJECT
Q_PROPERTY(QQuickItem* target READ target WRITE setTarget NOTIFY targetChanged)
public:
MouseProxyAttached(QObject* parent = 0);
static MouseProxyAttached* qmlAttachedProperties(QObject *object);
QQuickItem* target() const;
void setTarget(QQuickItem* target);
protected:
bool eventFilter(QObject *obj, QEvent *event);
signals:
void targetChanged(QQuickItem* target);
private:
QQuickItem* m_item = nullptr;
QQuickItem* m_target = nullptr;
};
QML_DECLARE_TYPEINFO(MouseProxyAttached, QML_HAS_ATTACHED_PROPERTIES)
#endif // MOUSEPROXYATTACHED_H
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment