How can I detect collision among QPixmaps and QGraphicsItems objects
-
I have QGraphicsEllipse items as bullets in a scene. Targets are QPixmap images and I want just the bullets and images to interact, NOT target on target collision. Bullets are created in my scene class and QPixmaps are created in my dialog class.
This deletes my bullets that leave outside of my sceneRect. I tried adding a QList for the QPixmaps created similar to the QList QGraphicsItem * but don't think the compiler liked that. Is there a way to gather all pixmaps in scene and only have them detect bullets. Also tried collidesWithItem with no luck. Any suggestions would be appreciated.
@void Scene::advance()
{
QList <QGraphicsItem *> itemsToRemove;
foreach( QGraphicsItem * item, this->items())
{if( !this->sceneRect().intersects(item->boundingRect())) { // The item is no longer in the scene rect, get ready to delete it itemsToRemove.append(item); } } foreach( QGraphicsItem * item, itemsToRemove ) { this->removeItem(item); delete(item); } QGraphicsScene::advance();
}@
-
It should be QGraphicsPixmapItems (or similar), not QPixmap.
Collision detection can only work between subclasses of QGraphicsItem. -
Yes, it should work that way.
-
I understand what you're saying about collision but the issues is I have the images stored in an array.
@ main_targets[0] = QPixmap(":images/darkbluelogo.jpg");
main_targets[1] = QPixmap(":images/graylogo.jpg");
main_targets[2] = QPixmap(":images/lightbluelogo.jpg");
main_targets[3] = QPixmap(":images/lime.jpg");
main_targets[4] = QPixmap(":images/pink.jpg");
main_targets[5] = QPixmap(":images/purple.jpg");
main_targets[6] = QPixmap(":images/redlogo.jpg");
main_targets[7] = QPixmap(":images/yellow.jpg");
main_targets[8] = QPixmap(":images/brown.jpg");@I ran into alot of errors trying to store this array in a QGraphicsPixmapItem
-
How did you define main_targets?
Is it
@
QVector<QPixmap> main_targets;
or
QPixmap main_targets[9];
@Qt specific keyword foreach works only with container classes like first definition. If you have second definition then you should use standard C++ for or while loop.
-
Hi,
foreach must be used with a containers. A c array is not considered a container.
In your case you can use a classic for loop or change your array to a QList of QPixmap
Hope it helps
-
I thought the @QVector<QGraphicsPixmapItem*> targetItems;
foreach(QPixmap pix, main_targets) {
QGraphicsPixmapItem* item = QGraphicsPixmapItem(pix);
targetItems.append(item);
}@ takes each instance of the main_targets array, and creates QGraphicsPixmapItems of them. -
I pass the pmap to the MainTarget class by pointer, which was previously by reference. There isn't a specific line that the compiler is showing. It crashes on execution. I replaced main_targets[index] with targetItems since that should hold the QGraphicsPixmapItem items.
@void Dialog::setPixmaps()
{
//Targets are delivered every 1sec
Five_timer->start(1000);QList<QGraphicsPixmapItem*> targetItems; foreach(QPixmap pix, main_targets) { QGraphicsPixmapItem* p_item = new QGraphicsPixmapItem(pix); targetItems.append(p_item); } int StartY = 0; //Random starting Y point between -140 and -389 StartY = (qrand() % (-400 + 150) -140); index = (qrand() % 9); { if(index % 2 == 0) { //When even is selected start at far right pmap = new MainTargets(targetItems[index], 0); pmap->setPos(345,StartY); } else { //When odd is selected start at far left pmap = new MainTargets(targetItems[index], 1); pmap->setPos(-325,StartY); } } scene->addItem(pmap);
}@
-
Guess that is where the problem is I thought the @ QList<QGraphicsPixmapItem*> targetItems;
foreach(QPixmap pix, main_targets) {
QGraphicsPixmapItem* p_item = new QGraphicsPixmapItem(pix);
targetItems.append(p_item);@ fills the main_target list. If not what is the purpose of this line? -
@ main_targets[0] = QPixmap(":images/darkbluelogo.jpg");
main_targets[1] = QPixmap(":images/graylogo.jpg");
main_targets[2] = QPixmap(":images/lightbluelogo.jpg");
main_targets[3] = QPixmap(":images/lime.jpg");
main_targets[4] = QPixmap(":images/pink.jpg");
main_targets[5] = QPixmap(":images/purple.jpg");
main_targets[6] = QPixmap(":images/redlogo.jpg");
main_targets[7] = QPixmap(":images/yellow.jpg");
main_targets[8] = QPixmap(":images/brown.jpg");@The main_targets are created and stored in the constructor initially before anything is done. So the qlist should have 9 qpixmaps.