class MainWindow : public QWidget { Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); private: QGraphicsScene *m_scene; QGraphicsRectItem *m_rect; QGraphicsItem *m_cross; QGraphicsView * graphicsView; };
class CrossItem: public QGraphicsItem { public: QRectF boundingRect() const { return QRectF(0, 0, 30*scale(), 30*scale()); } void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) { Q_UNUSED(option) Q_UNUSED(widget); painter->setPen(QColor(Qt::red)); painter->drawLine(10, 0, 20, 0); painter->drawLine(20*scale(), 0*scale(), 20*scale(), 10*scale()); painter->drawLine(20, 10, 30, 10); painter->drawLine(30, 10, 30, 20); painter->drawLine(30, 20, 20, 20); painter->drawLine(20, 20, 20, 30); painter->drawLine(20, 30, 10, 30); painter->drawLine(10, 30, 10, 20); painter->drawLine(10, 20, 0, 20); painter->drawLine( 0, 20, 0, 10); painter->drawLine( 0, 10, 10, 10); painter->drawLine(10, 10, 10, 0); } }; MainWindow::MainWindow(QWidget *parent) : QWidget(parent) { setLayout(new QGridLayout()); graphicsView = new QGraphicsView(); layout()->addWidget(graphicsView); graphicsView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); graphicsView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); m_scene = new QGraphicsScene(); m_cross = new CrossItem(); m_scene->addItem(m_cross); // m_cross->setScale(2); m_rect = new QGraphicsRectItem(m_cross->boundingRect(), m_cross); m_scene->addItem(m_rect); m_scene->setSceneRect(m_rect->boundingRect()); graphicsView->setScene(m_scene); } MainWindow::~MainWindow() { }
painter->drawLine(20*scale(), 0*scale(), 20*scale(), 10*scale());
return QRectF(0, 0, 30*scale(), 30*scale());
struct QGraphicsItemPrivate::TransformData { QTransform transform; qreal scale; qreal rotation; qreal xOrigin; qreal yOrigin; QList<QGraphicsTransform *> graphicsTransforms; bool onlyTransform; TransformData() : scale(1.0), rotation(0.0), xOrigin(0.0), yOrigin(0.0), onlyTransform(true) { } QTransform computedFullTransform(QTransform *postmultiplyTransform = 0) const { if (onlyTransform) { if (!postmultiplyTransform || postmultiplyTransform->isIdentity()) return transform; if (transform.isIdentity()) return *postmultiplyTransform; return transform * *postmultiplyTransform; } QTransform x(transform); if (!graphicsTransforms.isEmpty()) { QMatrix4x4 m; for (int i = 0; i < graphicsTransforms.size(); ++i) graphicsTransforms.at(i)->applyTo(&m); x *= m.toTransform(); } x.translate(xOrigin, yOrigin); x.rotate(rotation); x.scale(scale, scale); x.translate(-xOrigin, -yOrigin); if (postmultiplyTransform) x *= *postmultiplyTransform; return x; } };
#include "MainWindow.h" #include <QGridLayout> #include <QGraphicsSceneHoverEvent> #include <QDebug> class MyRect: public QGraphicsRectItem { public: MyRect(const QRectF &rect, QGraphicsItem *parent=0):QGraphicsRectItem(rect, parent){} protected: void hoverMoveEvent(QGraphicsSceneHoverEvent *event) { qDebug()<<"rect"<<event->pos(); QGraphicsRectItem::hoverMoveEvent(event); } }; class CrossItem: public QGraphicsItem { public: QRectF boundingRect() const { return QRectF(0, 0, 30, 30); } void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) { Q_UNUSED(option) Q_UNUSED(widget); painter->setPen(QColor(Qt::red)); painter->drawLine(10, 0, 20, 0); painter->drawLine(20, 0, 20, 10); painter->drawLine(20, 10, 30, 10); painter->drawLine(30, 10, 30, 20); painter->drawLine(30, 20, 20, 20); painter->drawLine(20, 20, 20, 30); painter->drawLine(20, 30, 10, 30); painter->drawLine(10, 30, 10, 20); painter->drawLine(10, 20, 0, 20); painter->drawLine( 0, 20, 0, 10); painter->drawLine( 0, 10, 10, 10); painter->drawLine(10, 10, 10, 0); } protected: void hoverMoveEvent(QGraphicsSceneHoverEvent *event) { qDebug()<<"cross"<<event->pos(); QGraphicsItem::hoverMoveEvent(event); } }; MainWindow::MainWindow(QWidget *parent) : QWidget(parent) { setLayout(new QGridLayout()); graphicsView = new QGraphicsView(); layout()->addWidget(graphicsView); graphicsView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); graphicsView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); m_scene = new QGraphicsScene(); m_cross = new CrossItem(); m_rect = new MyRect(m_cross->boundingRect(), m_cross); m_cross->setAcceptHoverEvents(true); m_rect->setAcceptHoverEvents(true); m_scene->addItem(m_cross); m_scene->setSceneRect(m_rect->boundingRect()); graphicsView->setScene(m_scene); } MainWindow::~MainWindow() { }
Leave your children to receive them. The parent doesn’t receive a case, however; the parent stays "hovered" until the cursor leaves its area, including its children's areas.
It’s not up to you to go for a child. children.
A QGraphicsWidget with window decorations will accept events regardless of the value of acceptHoverEvents ().
m_cross->setFiltersChildEvents(true);
bool sceneEventFilter(QGraphicsItem *watched, QEvent *event) { if(event->type() == QEvent::GraphicsSceneHoverMove) qDebug()<<"cross"<<event; return false; }
Source: https://habr.com/ru/post/183432/
All Articles