View Full Version : Custom items selection in qgraphicsscene
yonnak
26th March 2009, 19:33
Hello,
I'm trying to modify default selection behaviour in qgraphicsscene. I'd like to have an event propagation: if a mouse clic occurs on the topmost item, it should be sent to every item under the topmost one.
I try different solutions but can't get it work. Here is my last try in a class derived from QGraphicsScene:
bool myScene::event(QEvent *event)
{
if ( event->type() == QEvent::GraphicsSceneMousePress ) {
QGraphicsSceneMouseEvent * me = (QGraphicsSceneMouseEvent *) event;
me->setModifiers(Qt::ControlModifier);
for ( int i = 0; i < items(me->scenePos()).count(); i++ ){
items(me->scenePos()).at(i)->setFocus(Qt::MouseFocusReason);
mousePressEvent(me);
}
return false;
}
QGraphicsScene::event(event);
return true;
}
Thanks for any help
Lykurg
26th March 2009, 20:08
Hi, if you want to deliver events I don't know since graphicsitems has no notify(). [dirty: you could inherit QObject in each item, then use notify] But if you only want to select the items why not just use setSelected:
bool myScene::event(QEvent *event)
{
if ( event->type() == QEvent::GraphicsSceneMousePress ) {
QGraphicsSceneMouseEvent * me = (QGraphicsSceneMouseEvent *) event;
// test if right or left button is used
for ( int i = 0; i < items(me->scenePos()).count(); i++ ){
items(me->scenePos()).at(i)->setSelected(true); //<--
}
return false;
}
QGraphicsScene::event(event);
return true;
}
And better cache items(me->scenePos()) instead of performing the "search" in each for-run.
yonnak
26th March 2009, 20:22
Hello, thank you for replying.
I have an eventfilter on the items, depending where the clic occurs on the item I move or resize it. So I need to have a mouse event on every item, not only a selection behaviour.
And I will cache the item before the loop! :)
aamer4yu
26th March 2009, 20:48
Still what do u want to achieve with the items below the top item ? What behaviour you want ?
might be it can be done some other way than modifying the current behaviour of graphics view framework :rolleyes:
yonnak
27th March 2009, 07:39
Yes you are right, there is probably a best way to do what I want.
So here is what i have:
I add line items on my scene, if I clic an extremity of the line, I can rotate/resize it and when I clic any other point of the line, I move it around my scene.
What I want:
If two lines are contiguous, I'd like that clicking on the intersection of the two lines permit to rotate/resize the 2 lines at a time.
If you have a good way to do this...
Lykurg
28th March 2009, 07:08
Hi,
I just come across QCoreApplication::postEvent ( QObject * receiver, QEvent * event ). May it help you.
Lykurg
aamer4yu
28th March 2009, 09:04
There was a FotoWall example on Qt-apps.org . Search and read the code.
It had features of resizing and rotating graphics item
As for ur case, moving is not a problem, graphics item can move in a scene.. you only need to enable flags for it :)
as for rotating, the fotowall example might help u :)
yonnak
28th March 2009, 11:32
Hi, thanks for replies!
I can't use QCoreApplication::postEvent ( QObject * receiver, QEvent * event ) since QGraphicsItem does not herites from QObject.
I will take a look at Fotowall, but I have already the resizing, rotating feature working on my items, what I want is to apply this feature at many items at the same time if items are colliding.
I will try again and let you if I find a solution to my problem.
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.