Search code examples
c++qtvideoqwidgetqmediaplayer

Drawing on a qwidget while playing videos


I have a class A that inherits from a class that inherits from QWidget. My class contains a media player and a QVideoWidget. I want my object A to draw a small rectangle for a few milliseconds every time a new video from the play list starts playing, on the part of the widget that isn't the video widget. So, I've connected my slot

connect(player, &QMediaPlayer::currentMediaChanged,this,&videoDisplay::drawRect);

but I can't manage to get the drawRect function right. Tried using QPainter but it gave me errors

QWidget::paintEngine: Should no longer be called
QPainter::begin: Paint device returned engine == 0, type: 1

any advice?

Thanks


Solution

  • Painting in Qt should generally only be performed as the result of an update request.

    Rather than try to draw directly you could use a flag or similar to signify a new item has started from the playlist...

    bool m_new_video;
    

    Then just use a lambda as the slot and have it set the flag and request an update...

    connect(player, &QMediaPlayer::currentMediaChanged,
            [this]()
            {
              m_new_video = true;
              update();
            });
    

    Your videoDisplay::paintEvent implementation can then draw whatever it has to based on the value of m_new_video.