Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QGraphicsItem registers hover and click events only in parts of the shape



  • I am reimplementing QGraphicsItem as a line between two points. In the picture, the black line is the actual line, the red lines are the shape, and the blue rectangle is boundingRect. I've reimplemented QGraphicsView, but I call base functions when I override them.

    Here is the weird thing, mousePressEvent is registered in all of the bounding rect, but not in the top right corner, and hoverEnterEvent is registered only in bottom third of the shape. I have no idea what is going on.

    relevant code below
    Whole project: github.com/Makimars/Konstructor

    Screenshot from 2020-03-12 21-57-42.png

    QRectF Line::boundingRect() const
    {
            // getLocation() returns QPointF
    	return QRectF(this->startPoint->getLocation(),this->endPoint->getLocation());
    }
    
    QPainterPath Line::shape() const
    {
    	QVector2D lineVector = this->getLineVector().normalized();
    	QVector2D normalVector(
    				-lineVector.y(),
    				lineVector.x()
    						);
    	normalVector *= 4;
    	QPointF startPointOne(this->startPoint->getLocation()+normalVector.toPointF());
    	QPointF startPointTwo(this->startPoint->getLocation()-normalVector.toPointF());
    
    	QPointF endPointOne(this->endPoint->getLocation()+normalVector.toPointF());
    	QPointF endPointTwo(this->endPoint->getLocation()-normalVector.toPointF());
    
    	QPolygonF polygon;
    	polygon << startPointOne << startPointTwo << endPointTwo << endPointOne << startPointOne;
    
    	QPainterPath path;
    	path.addPolygon(polygon);
    
    	return path;
    }
    
    void Line::paint(QPainter *painter,
    				 const QStyleOptionGraphicsItem *option,
    				 QWidget *widget)
    {
    	if(this->isHidden())
    		return;
    
    	DrawableObject::paint(painter, option, widget);
    
    	painter->drawLine(this->startPoint->getLocation(),
    						this->endPoint->getLocation()
    					);
    }
    
    void DrawableObject::paint(QPainter *painter,
    						   const QStyleOptionGraphicsItem *option,
    						   QWidget *widget)
    {
    
    	if(this->highlight)
    		this->pen->setWidth(2);
    	else
    		this->pen->setWidth(1);
    
    	painter->setPen(*this->pen);
    
    	painter->setPen(Qt::blue);
    	painter->drawRect(boundingRect());
    	painter->setPen(Qt::red);
    	painter->drawPath(shape());
    	painter->setPen(Qt::black);
    }
    

Log in to reply