PDA

View Full Version : mouse events handling with QGraphicsItem



trallallero
21st October 2009, 12:08
Hallo all,

I have a strange problem with the mouse events handling in QGraphicsView and QGraphicsItem.

I create a:
"class MyGraphicsView : public QGraphicsView"
that has a function called:
"void addButton (tButtonValues& vals);"



void MyGraphicsView::addButton(tButtonValues& vals)
{

MyButton* btn = new MyButton(vals, MyFrame::GetFrame(vals.Owner));

vals.MySelf = btn;
if (vals.Owner < 0) // the button has no owner so it has not been added to the scene yet
m_Scene->addItem(btn);

btn->grabMouse();
}

The problem is that I add 16 buttons to the view but only the last one grabs the mouse.

In MyButton class I have this method:


void MyButton::mouseReleaseEvent ( QGraphicsSceneMouseEvent* event )
{
printf("mouse RELEASE event - <%i> - <%i>\n", event->pos().toPoint().x(), event->pos().toPoint().y() );

QRect r( mVals.qpPosition, mVals.qsSize );

if ( r.contains(event->pos().toPoint()) )
{
puts("OK");
}
QGraphicsItem::mouseReleaseEvent(event);
}

I try to propagate the event to all the other items with a mouseReleaseEvent call but no way, only the last created button gets the call.
Is it a bug or am I doing something wrong ?

Thanks

scascio
21st October 2009, 14:30
I am not using grabMouse like you did
but what I read about that is that on widget at a time can grab the mouse.

So I am afraid that you can't do what you want that way.

trallallero
21st October 2009, 14:49
I am not using grabMouse like you did
but what I read about that is that on widget at a time can grab the mouse.

So I am afraid that you can't do what you want that way.

Yes, I've just found the problem :)

grabMouse cannot be used, you're right, but my problem was in boundingRect method.
I returned:

QRectF boundingRect () const { return QRectF(); }
and this of course means that the buttons are 0 sized.

Now I return:

QRectF boundingRect () const { return QRectF(QPoint(0, 0), mVals.qsSize); }

and everything works fine :D

Thanks anyway.

trallallero
21st October 2009, 15:15
I've forgotten on thing:
If these flags are not set, it doesn't work !


setFlags(QGraphicsItem::ItemIsFocusable | QGraphicsItem::ItemIsSelectable);
O at least ItemIsSelectable I think.