Drawing overlay on QVideoWidget (5.15.2)
-
How can I draw a live animated overlay on a
QVideoWidget?I tried subclassing it and overriding
paintEvent, but it appears that paint events aren't actually used to updateQVideoWidget, as the event handler is never called while the video is playing. (My intent with that was to just have aQImagewith an alpha channel for the overlay, and draw it on top of the video at every paint event.)I also found this forum post, but, unless I'm not fully understanding the solution, the solution that the OP ultimately found there only seems appropriate for static, unchanging overlay content.
How is the
QVideoWidgetrendering video (it's clearly not using standard paint events) and how can I draw overlays with dynamic content on top of it? -
I figured it out; I just switched to using a
QGraphicsView.Then, the video can be rendered to a
QGraphicsVideoItemand overlay can be set by putting other graphics items (likeQGraphicsPixmapItem) on top of it.The
QGraphicsVideoItem::nativeSizeChangedsignal can be used to drive any graphics item sizing logic that depends on the video size.E.g.
scene_ = new QGraphicsScene(this); scene_->addItem(video_ = new QGraphicsVideoItem()); scene_->addItem(overlay_ = new QGraphicsPixmapItem()); ui_->view->setScene(scene_); connect(video_, &QGraphicsVideoItem::nativeSizeChanged, this, [this](QSizeF size) { video_->setSize(size); ui_->view->fitInView(video_, Qt::KeepAspectRatio); });And, when setting the overlay:
QImage image = ...; /* some dynamically generated QImage */ overlay_->setPixmap(QPixmap::fromImage(image)); QTransform tx = QTransform::fromScale(video_->size().width() / image.width(), video_->size().height() / image.height()); overlay_->setTransform(tx);And of course
fitInViewcalls on the window's resize and show events as usual.