我有一个QGraphicsPixmapItem,可以在不同的像素图中旋转来模拟动画。我需要准确地实现shape()函数,这样场景才能正确地确定与其他对象的碰撞。显然,每个像素图都有稍微不同的碰撞路径。有没有一种简单的方法可以通过勾勒出实际图像的彩色像素来从像素图中创建QPainterPath,而不必编写我自己的复杂算法来尝试手动创建该路径?
我计划预先绘制这些路径,并以与像素图相同的方式循环它们。
发布于 2014-04-14 01:19:31
您可以将QGraphicsPixmapItem::setShapeMode()与QGraphicsPixmapItem::MaskShape或QGraphicsPixmapItem::HeuristicMaskShape一起使用,以实现以下目的:
#include <QtGui>
#include <QtWidgets>
class Item : public QGraphicsPixmapItem
{
public:
Item() {
setShapeMode(QGraphicsPixmapItem::MaskShape);
QPixmap pixmap(100, 100);
pixmap.fill(Qt::transparent);
QPainter painter(&pixmap);
painter.setBrush(Qt::gray);
painter.setPen(Qt::NoPen);
painter.drawEllipse(0, 0, 100 - painter.pen().width(), 100 - painter.pen().width());
setPixmap(pixmap);
}
enum { Type = QGraphicsItem::UserType };
int type() const {
return Type;
}
};
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QGraphicsView view;
view.setScene(new QGraphicsScene());
Item *item = new Item();
view.scene()->addItem(item);
// Comment out to see the item.
QGraphicsPathItem *shapeItem = view.scene()->addPath(item->shape());
shapeItem->setBrush(Qt::red);
shapeItem->setPen(Qt::NoPen);
view.show();
return app.exec();
}https://stackoverflow.com/questions/23045258
复制相似问题