I have a QGraphicsPixmap item in a QGraphicsScene. The item has flags set to ItemIsMovable, and ItemIsSelectable. How do I ensure that when the item is moved out of a certain boundary - it can be a QGraphicsScene or just a fixed frame size at fixed coordinates - the part becomes hidden?

Eg. enter image description here

The left part of the basketball becomes hidden.


你必须使用setClipPath().

在下面的代码中,我创建了一个继承自的类QGraphicsPixmapItem(对于继承自的其他类也可以这样做),并且我创建了接收指示可见区域QGraphicsItem的方法,例如在代码中使用:setBoundaryPath()QPainterPath

QPainterPath path;
path.addRect(QRectF(100, 100, 400, 200));

(100, 100)QPainterPath 是一个矩形,其左上角是QGraphicsScene 的点,400宽度和200高度的大小。

#include <QApplication>
#include <QGraphicsRectItem>
#include <QGraphicsView>

class GraphicsPixmapItem: public QGraphicsPixmapItem{

public:
    GraphicsPixmapItem(const QPixmap & pixmap,  QGraphicsItem *parent = 0):
        QGraphicsPixmapItem(pixmap, parent)
    {
        setFlag(QGraphicsItem::ItemIsMovable, true);
        setFlag(QGraphicsItem::ItemIsSelectable, true);
    }
    void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){
        if(!m_boundaryPath.isEmpty()){
            QPainterPath path = mapFromScene(m_boundaryPath);
            if(!path.isEmpty())
                painter->setClipPath(path);
        }
        QGraphicsPixmapItem::paint(painter, option, widget);
    }

    QPainterPath boundaryPath() const{
        return m_boundaryPath;
    }
    void setBoundaryPath(const QPainterPath &boundaryPath){
        m_boundaryPath = boundaryPath;
        update();
    }

private:
    QPainterPath m_boundaryPath;
};

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QGraphicsView view;
    QGraphicsScene scene(0, 0, 600, 400);
    view.setScene(&scene);
    view.setBackgroundBrush(QBrush(Qt::gray));

    GraphicsPixmapItem *p_item = new GraphicsPixmapItem(QPixmap(":/ball.png"));
    p_item->setPos(100, 100);

    // Define the area that will be visible
    QPainterPath path;
    path.addRect(QRectF(100, 100, 400, 200));

    p_item->setBoundaryPath(path);
    scene.addItem(p_item);

    // the item is added to visualize the intersection
    QGraphicsPathItem *path_item = scene.addPath(path, QPen(Qt::black), QBrush(Qt::white));
    path_item->setZValue(-1);
    view.show();
    return a.exec();
}

在此处输入图像描述

在此处输入图像描述

您可以在此链接中找到示例代码


你可以更好地解释你自己,我不明白你

我添加了更多信息。哪一部分不清楚?

您希望仅当它在某个区域(例如矩形)内时才显示它,如果它在区域外,则该部分被剪切。我是对的?

对,那是正确的。

I wonder what's the certain boundary which the OP means. Why not just use a layer contains transparent stack on the basketball...

I've edited the question to clarify certain boundary. There is no fixed approach at the moment.

@ALH a QGraphicSscene is everyone in QGraphicsView, that has no limits, explain how you determine that area, do not confuse sceneRect () with QGraphicsScene. in my example I add an item so that the area is visible but you can delete the path_item, in my example it is determined by the QPainterPath.

@ALH I have added a clearer explanation to my answer, please review it.