Pregunta

My class structure is:

A class called FloatingButtonItem , inherits QGraphicsPixmapItem.

class FloatingButtonItem : public QGraphicsPixmapItem
{

(...) And reimplements these five functions.

protected:

void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
void mousePressEvent(QGraphicsSceneMouseEvent *event);
void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
void hoverEnterEvent(QGraphicsSceneHoverEvent *event);
void hoverLeaveEvent(QGraphicsSceneHoverEvent *event);

Each of them just change the value of a BOOLEAN variable and print a message.

And in the constructor i have:

FloatingButtonItem::FloatingButtonItem(MyGraphicsScene* scene,  QGraphicsItem* parent) :  QGraphicsPixmapItem(parent)

The FloatingButton gets painted perfectly but the hover events are not received.

And before you ask it: YES, i added this line on the creation:

  **setAcceptHoverEvents(true);**
  setFlag(ItemIsMovable, true);
  setFlag(ItemSendsGeometryChanges, true);
  setZValue(20.0);

And the item appears in front of the rest of the items perfectly.

After creating it, i add it to the scene by using "addItem" function.

Is it any problem with the creation? With the parenting?

On the other hand I have another class that inherits from QGraphicsPolygonItem, and what i called GenericItem, a class that is created like this:

class GenericItem : public QObject {

Q_OBJECT

...

And it DOES work with hover events. Apparently it is the same... Adding Q_OBject and making it inherit from QObject doesn't work.

EDIT: I add another comment: I checked the bounding rectangle. It is ok. The size is not 0.

EDIT 2: Concerning the bounding box:

I reimplemented boundingRect function too:

QRectF boundingRect () const { return boundingRect_; }   

And bounding rect is calculated in the creation taking the Pixmap selected like this:

boundingRect_ = QRectF( -pix.width()/2, -pix.height()/2, pix.width() , pix.height());
setPos(-pix_.width()/2,-pix_.height()/2);

Any ideas?

¿Fue útil?

Solución

You state that this is the constructor for your class: -

FloatingButtonItem::FloatingButtonItem(MyGraphicsScene* scene,  QGraphicsItem* parent) 
      :  QGraphicsPixmapItem(parent,scene)

If you look at the constructors for QGraphicsPixmapItem, the documentation shows these: -

QGraphicsPixmapItem(QGraphicsItem * parent = 0)
QGraphicsPixmapItem(const QPixmap & pixmap, QGraphicsItem * parent = 0)

Neither constructor take a QGraphicsScene as the 2nd parameter, which is what you're passing to the base class.

You should be creating your pixmap item and calling: -

pScene->addItem(floatingButtonItem);
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top