Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QGraphicsItem registers hover and click events only in parts of the shape

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

Scheduled Pinned Locked Moved Unsolved General and Desktop
1 Posts 1 Posters 298 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Makimars
    wrote on last edited by
    #1

    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);
    }
    
    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved