Search code examples
qtqgraphicsviewqmediaplayer

Qt QGraphicsView not playing any video


I have a very simple QT 5.11.0 application with a graphicsview that i would like to play a video in.

Here is my code, it compiles, loads and displays a blank graphicsview.

#include "Demo_TeleLink.h"


Demo_TeleLink::Demo_TeleLink(QWidget *parent)
    : QMainWindow(parent)
{
    ui.setupUi(this);

    scene = new QGraphicsScene;
    player = new QMediaPlayer();
    videoItem = new QGraphicsVideoItem;
    newString = "C://Users//Chris//Desktop//Sample1.mp4";

    ui.graphicsView->setScene(scene);

    player->setVideoOutput(videoItem);

    ui.graphicsView->scene()->addItem(videoItem);

    player->setMedia(QUrl(newString));

    ui.graphicsView->fitInView(videoItem);

    player->play();
}

All the required objects are declared in the header as points if needed.

enter image description here


Solution

  • The QUrl("/path/of/video") is not a valid url since the scheme file that indicates that it is a local file is not deduced, there are 2 possible solutions to this:

    player->setMedia(QUrl::fromLocalFile(newString));
    

    or

    player->setMedia(QUrl::fromUserInput(newString));
    

    Maybe the path is not encoded correctly, try using the following code and selecting the video manually

    newString = QFileDialog::getOpenFileName(this,
                                             tr("Open Video"),
                                             QDir::homePath(),
                                             tr("Video Files (*.mp4)"));
    

    The OP indicates that it obtains the following error message:

    DirectShowPlayerService::doRender: Unresolved error code 0x80040266 (IDispatch error #102)
    

    And according to the QTBUG-52082 reported, the solution is to install the codecs to play mp4 from http://www.codecguide.com/download_kl.htm