如何在QGraphicsItem的可移动区域周围绘制一个边界矩形?

huangapple go评论86阅读模式
英文:

How to draw a bounding rect around movable area of QGraphicsItem?

问题

我创建了一个自定义的QGraphicsItem,并重写了paint函数来绘制图像,这个图像是ghost.png,是一个具有复杂边界的角色,可以通过点击图像的///四个边来移动。

我理解这是因为boundingRect的设置,我将其设置得更大,因为我需要绘制其他内容,比如文本、生命值条等等...

如何保持较大的boundingRect,同时限制只有在图像上点击时才能移动图像?

我的类:

class DrawImage : public QGraphicsItem
{
public:
    DrawImage();
    
protected:
    QRectF boundingRect() const override;
    void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override;
};

我的定义:

DrawImage::DrawImage()
{
    setFlag(ItemIsMovable);
}

QRectF DrawImage::boundingRect() const
{
    return QRectF(0, 0, 100, 200);
}

void DrawImage::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    auto ghost = QImage("ghost.png");
    painter->drawImage(QRect(0, 0, 100, 100), ghost);
}
英文:

I made a custom QGraphicsItem and I am overriding the paint function to draw image which is ghost.png, a character that has a complex boundary, and can be moved by clicking on lower/upper/ left/right side of the image.

I understand this is happening because of the boundingRect, I am setting it larger because I need to draw something else, for example: text, health bar, etc...

How can I keep a larger boundingrect and also limit the image to move only if clicked on the image.

My class :

class DrawImage : public QGraphicsItem
{
public:
    DrawImage();

protected:
    QRectF boundingRect() const override;
    void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override;
};

My Definition :

DrawImage::DrawImage()
{
    setFlag(ItemIsMovable);
}

QRectF DrawImage::boundingRect() const
{
    return QRectF(0, 0, 100, 200);
}

void DrawImage::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    auto ghost = QImage(":ghost.png");
    painter->drawImage(QRect(0, 0, 100, 100), ghost);
}

答案1

得分: 2

musicamante 说道:

>重写 [QGraphicsItem::]shape

以下是如何使你的 QGraphicsPixmapItem 仅在点击其 pixmap 时可移动,并在其周围有一个 boundingRect 的示例:

class DrawImage : public QGraphicsItem
{
public:
    DrawImage()
    {
        setFlag(ItemIsMovable);
    }

protected:
    QRectF boundingRect() const override
    {
        return QRectF(0, 0, 100, 200);
    }

    void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override
    {
        auto greenBall = QPixmap("green.svg");
        QRect rect = boundingRect().toRect();

        // 你可以调整 pixmap 的绘制位置
        // 我选择了 bounding rect 的上半部分
        rect.adjust(0,0,0,-rect.height()/2);
        painter->drawPixmap(rect, greenBall);

        // 为了可见性,我绘制了整个 bounding rect
        painter->setPen(Qt::green);
        painter->drawRect(boundingRect());
    }

    QPainterPath shape() const
    {
        // 这里你可以指定用户点击哪个区域移动对象
        auto greenBall = QPixmap("green.svg");
        QRect rect = boundingRect().toRect();
        
        // 确保它与你绘制 pixmap 的区域相同
        rect.adjust(0,0,0,-rect.height()/2);
        QPainterPath path;
        // 将其添加为你想要的形状,我选择了绿色圆圈的椭圆形状
        path.addEllipse(rect);
        return path;
    }
};

这是结果图:

如何在QGraphicsItem的可移动区域周围绘制一个边界矩形?

查看源代码以获取更多详细信息:

英文:

musicamante said:

>Override [QGraphicsItem::]shape.

Here's how you can make your QGraphicsPixmapItem moveable only when clicked on its pixmap, and have a boundingRect around it:

class DrawImage : public QGraphicsItem
{
public:
    DrawImage()
    {
        setFlag(ItemIsMovable);
    }

protected:
    QRectF boundingRect() const override
    {
        return QRectF(0, 0, 100, 200);
    }

    void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override
    {
        auto greenBall = QPixmap(":green.svg");
        QRect rect = boundingRect().toRect();

        //You can adjust where your pixmap is drawn
        //I chose the upper part of the bounding rect
        rect.adjust(0,0,0,-rect.height()/2);
        painter->drawPixmap(rect, greenBall);

        //I'm drawing the full bounding rect just to make it visible
        painter->setPen(Qt::green);
        painter->drawRect(boundingRect());
    }

    QPainterPath shape() const
    {
        //this is where you sepecify which area of your object allows the user to move it when clicked
        auto greenBall = QPixmap(":green.svg");
        QRect rect = boundingRect().toRect();
        
        //Make sure it is the same area where you're drawing your pixmap
        rect.adjust(0,0,0,-rect.height()/2);
        QPainterPath path;
        //add it as the shape you want, I chose an ellipse for the green circle
        path.addEllipse(rect);
        return path;
    }
};

Here's the result:

如何在QGraphicsItem的可移动区域周围绘制一个边界矩形?

See the source code for more details:

huangapple
  • 本文由 发表于 2023年6月12日 02:03:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76451832.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定