display image in qvideowidget qmediaplayer possible?

  • Hi there,
    I have a QWidget that contains just a QMediaPlayer and a QVideoWidget to play some video.
    Is it possible to initially show an image in the QVideoWidget until I start the player video?

    QVideoWidget* vw = new QVideoWidget();
    QMediaPlayer* mplayer = new QMediaPlayer;

    I just get a "Error: "No valid frames decoded before end of stream""

  • Lifetime Qt Champion


    Aren't you trying to show an image rather than a video ?

  • Lifetime Qt Champion

    @SGaist Yes, he wants to show a picture before a video file is played

  • Lifetime Qt Champion

    @jsulm Oo, I've managed to miss that phrase…

    I'd rather use a QStackedLayout with a QLabel to show the image and when you want to start the video, switch to the QVideoWidget.

  • This might be totaly wrong way to do it but I just found another approach. I created a QWidget with a stylesheet background image and when I press "video start" I call the camera part but with the QCameraViewfinder parented to the QWidget. I had to add setGeometry to get proper display of the video.

    void AlignmentDialog::on_pushButton_video_start_clicked()
    	vf = new QCameraViewfinder(ui->widget_video);


  • @SGaist your suggestions seems more proper to me, thank you

  • Lifetime Qt Champion

    In your version you have two problems:

    1. You're leaking a QCameraViewFinder each time you click on the button
    2. Your view finder won't adapt if you change the size of your widget

    1. what do you mean with leaking? Memory leak? (Sry, I am very new to coding)
    2. The viewfinder should not change in size, I will make it static sized.

    EDIT: you mean I should check if a viewfinder is already launched?

  • Lifetime Qt Champion

    1. Each time you call that function you create a new view finder. It has a parent so it will be destroyed at the end but still, you're filling up memory with unused instances. Either check it has already been created or just create it in the constructor of your widget.

    2. You can also use setFixedSize for that.

  • thanks a lot :)

  • @pauledd

    If QMediaPlayer a fixed size then you can solve in a simple way as follows.

    like this:

    auto mplayer = new QMediaPlayer;
    auto vw = new QVideoWidget();
    auto pal = vw->palette();
    // Sets the brush pixmap to pixmap. The style is set to Qt::TexturePattern.
    pal.setBrush(QPalette::Window, QBrush(QPixmap(":/yourImage.jpg")));
    connect(mplayer, &QMediaPlayer::stateChanged, [vw](QMediaPlayer::State state){
                        if (state == QMediaPlayer::StoppedState)
                            vw->update(); });

    If resizing is enabled will cause a very interesting one. :)

  • thank you for the tip, I will try this in another project since I currently have a lot of problems getting qmediaplayer or qcamera working on the RaspberryPi. I wrote my code on amd64 on which everything worked fine but on the actual device (RPi) a LOT of problems came in (qt-gstreamer-v4l2-bcm2835-v4l2,MMAL, w.t.h.) but thats worth another thread.