PDA

View Full Version : QGraphicsItem hover event



xgoan
7th December 2006, 10:57
Hi. I'm subclassing a QGraphicsItem, but I can't recieve the hover events. I'm forgoting anything?

The code is below


#include "square.h"

#include <QtGui>

Square::Square(QPolygonF &polygon, uint row, uint col){
m_polygon=polygon;
m_row=row; m_col=col;
m_hover=false;
setAcceptsHoverEvents(true);
}

QRectF Square::boundingRect() const{
return m_polygon.boundingRect();
}

void Square::paint(QPainter *painter, const QStyleOptionGraphicsItem*, QWidget*){
if(m_hover) painter->setBrush(Qt::red);
painter->drawPolygon(m_polygon);
painter->drawText(boundingRect(), QString("%1x%2").arg(m_row).arg(m_col));
}

void Square::hoverEnterEvent(QGraphicsSceneHoverEvent*) {
m_hover=true;
}

void Square::hoverLeaveEvent(QGraphicsSceneHoverEvent*) {
m_hover=false;
}

Thanks, bye

jpn
7th December 2006, 11:21
Are you sure you are not receiving them? Maybe you should call QGraphicsItem::update() to schedule a repaint of the item?

xgoan
7th December 2006, 11:26
Yes, I have test it using QMessageBox::information... And don't works, but if I use mousePressEvent it works

aamer4yu
7th December 2006, 11:37
I think u just need to check if the item has mouse over it...

u can do it by the following


void Square::paint(QPainter *painter, const QStyleOptionGraphicsItem*, QWidget*)
{
if(option->state & QStyle::State_MouseOver) painter->setBrush(Qt::red);
painter->drawPolygon(m_polygon);
painter->drawText(boundingRect(), QString("%1x%2").arg(m_row).arg(m_col));
}

if(option->state & QStyle::State_MouseOver)

hope it helps :0

xgoan
7th December 2006, 12:02
Don't work :(

aamer4yu
8th December 2006, 03:53
can we see the changed code u wrote ??
are u still using m_hover to check the hover event ?? and have u set setAcceptsHoverEvents(true); ???

veiovis
4th January 2016, 15:08
can we see the changed code u wrote ??
are u still using m_hover to check the hover event ?? and have u set setAcceptsHoverEvents(true); ???

This solved it for me.