c++ - QGraphicsItem绘图问题

标签 c++ qt qgraphicsview

为了学习 Qt,我正在开发一款小型塔防游戏。我正在使用 QGraphicsScene 来保存游戏的所有对象。为了让它们移动,我没有使用动画框架,而是调用了 advance() 方法和 QTimer。

我想让我的射弹在击中敌人时爆炸。问题是当我试图绘制一个椭圆来模拟爆炸时,它没有被正确绘制。

您可以在 this video 中看到问题.

我尝试使用 z-indexes 但它没有改变任何东西。

这是我用来绘制射弹的代码:

void Projectile::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    if(!isExploding) {
        painter->drawPixmap(boundingRect().toRect(), image);
    } else {
        if(cnt < 50) {
            painter->setBrush(QBrush(explosion));
            painter->drawEllipse(-cnt, -cnt, 2.0*cnt, 2.0*cnt);
            cnt++;
        } else {
            this->isFinished = 1;
        }
    }
}

QRectF Projectile::boundingRect() const
{
    // Taille de l'image de l'insecte
    return QRectF(0, 0, 6, 6);
}

关于如何解决这个问题,你有什么线索吗?

谢谢。

最佳答案

假设 cnt 是 3。 你正在画一个椭圆

painter->drawEllipse(-3,-3,6,6)

这需要一个宽度和高度至少为 9 的 boundingRect。
boundingRect 也是使用内部项目坐标系指定的。您正在从 (-3,-3) 绘制到 (6,6),这是在 boundingRect 之外。

关于c++ - QGraphicsItem绘图问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6047985/

相关文章:

c++ - C中使用读写函数的套接字错误

c++ - QLabel 更改文本后未调整大小

qt4 - 调整 QGraphicsView 大小不会移动小部件

c++ - 在 Qt 中绘制绘图的最佳方法是什么?

python - 使 QGraphisItem 适合 View

c++ - C++/链表中的指针

c++ - 摆脱编译器警告 "warning: result of call is not used"

c++ - 使用惰性迭代器进行C++过滤

android - 使用 Qt5 为 iOS 和 Android 创建应用程序

使用 Qt 库的 C++ 应用程序在屏幕保护程序启动时停止工作