I have two GraphicsItem objects added to a GraphicsScene.
Their flags have been set to "ItemIsMovable", so the user can move them about in the scene.
How do I use the collision detection feature of Qt to check if there is a collision when the items are being moved by the user.
I know you have to use the collidesWith() method.
Not sure exactly how to use it,
here is my code so far:
@ QGraphicsPixmapItem *picture = new QGraphicsPixmapItem(QPixmap("C:/Users/Public/Pictures/images (2)"));
QGraphicsPixmapItem *picture2 = new QGraphicsPixmapItem(QPixmap("C:/Users/Public/Pictures/images (5)"));
picture2->setPos(500,500); picture->setFlag(QGraphicsItem::ItemIsMovable); picture2->setFlag(QGraphicsItem::ItemIsMovable); scene.addItem(picture); scene.addItem(picture2); qDebug()<< picture << endl << picture; QGraphicsView view(&scene); view.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform); view.show();@
Does the collision detection happen automatically behind the scenes and is there a signal generated
that I can slot into.
Oh crap graphics scene is not a descendant of QWidget or QObject, therfeore no signals and slots.
Which confuses me even more. How does it work then?
By the way all of that code is in main.cpp so I haven't sub-classed any thing
Ok so from reading a bit of the qt documentation I have figured out that I have to call someItem.collidingItems(), to give me a list of items that have collided with the item "someItem".
I think I am going to check if there is a collision everytime I release the mouse and then if I have moved one item over another there should be collision which I can respond to.
My problem is I don't know how to handle events outside of the signal slot paradigm.
I have only ever used signal and slots in my simple gui's to handle user clicks on a button or if a user toggles a checkbox etc.
With the Event system when a user actually drags an item and then releases it then how does the system handle the event because no signal is generated since GraphicsItems are not QObjects or QWidgets?
You can check with function collidesWithItem()
There is a nice example program (Diagram Scene) in examples ( file arrow.cpp, method paint).
It returns bool true if it does collide or false if it does not.
And in graphics scene you have signals and slots. You have change signal based on which you can process. Take a look at docs "here":http://qt-project.org/doc/qt-4.8/QGraphicsScene.html
If you want signals for collision, you'll have to derive QGraphicsScene class and re implement mouseMoveEvent() or similar function, and then emit the signal.
And next time please use the edit button and modify your last post if there are any changes.
Jake, you the man!
Yes Graphics Scene does have signals and slots and I am an idiot. I don't know how I came to the conclusion that it doesn't.
So now I have sub classed QGraphicsScene and I have re-implemented the
updateChange (const QList<QRect> rects) slot where I detect collisions by examining the area of the scene that has changed by getting all the items in the region that has changed and checking for any collisions.
I connected the sceneChanged(const QList<QRect> & rects) signal to it obviously.
Now I am going to Try and re-implement mouseMoveEvent()