我有两个QGraphicsPixmapItem,它们的边缘都是透明的,并且它们不是矩形的。当我尝试使用QGraphicsItem::collidingItems()时,它仅检查其边界矩形是否正在碰撞。有没有办法只检测非透明零件的碰撞?

最佳答案

您必须将形状模式设置为 QGraphicsPixmapItem::HeuristicMaskShape :


your_item->setShapeMode(QGraphicsPixmapItem::HeuristicMaskShape);

例:

main.cpp
#include <QApplication>
#include <QGraphicsPixmapItem>
#include <QGraphicsView>

#include <QDebug>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QGraphicsView w;
    QGraphicsScene *scene = new QGraphicsScene;
    w.setScene(scene);

    QList<QGraphicsPixmapItem *> items;

    for(const QString & filename: {":/character.png", ":/owl.png"}){
        QGraphicsPixmapItem *item = scene->addPixmap(QPixmap(filename));
        item->setShapeMode(QGraphicsPixmapItem::HeuristicMaskShape);
        item->setFlag(QGraphicsItem::ItemIsMovable, true);
        item->setFlag(QGraphicsItem::ItemIsSelectable, true);
        items<<item;
    }

    items[1]->setPos(50, 22);
    if(items[0]->collidingItems().isEmpty())
        qDebug()<<"there is no intersection";
    w.show();
    return a.exec();
}

c&#43;&#43; - 如果像素图的边缘是透明的,如何检测两个QGraphicsPixmapItem之间的冲突?-LMLPHP

输出:
there is no intersection

完整的示例可以在以下link中找到

10-08 11:24