Silverness
Silverness

Reputation: 97

Qt: How to draw a QRect on top of a QGraphicsVideoItem?

I have a subclass of a QGraphicsObject. In this class, I use a QMediaPlayer with a QGraphicsVideoItem to play a video. I am trying to draw on top of the video.

class MyClass : public QGraphicsObject
{
Q_OBJECT;

public slots:
    void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);

public:
    QGraphicsVideoItem *_movie_item;
    QMediaPlayer *_movie_player;
}

In the paint method, I want to draw a red rectangle on top of _movie_item. To try to do this, I call:

void MyClass::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    _movie_item->setVisible(true);
    _movie_item->play();
    painter->fillRect(startx, starty, size, size, Qt::red);
}

The red rectangle gets drawn in the appropriate place, but it is always underneath the _movie_item.

Is there a way to draw a rectangle on top of the _movie_item without creating another QGraphicsItem, like:

QGraphicsRectItem *_rect = new QGraphicsRectItem(_movie_item)

Thanks.

Upvotes: 2

Views: 691

Answers (1)

erdinc
erdinc

Reputation: 21

Using QGraphicsItem::ItemStacksBehindParent flag may help.

From the docs:

The item is stacked behind its parent. By default, child items are stacked on top of the parent item. But setting this flag, the child will be stacked behind it. This flag is useful for drop shadow effects and for decoration objects that follow the parent item's geometry without drawing on top of it. This flag was introduced in Qt 4.5.

_movie_item->setFlag(QGraphicsItem::ItemStacksBehindParent);

Upvotes: 2

Related Questions