Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Trying to play video/stream Fixing Error in QGraphicsVideoItem [SOLVED]
Forum Updated to NodeBB v4.3 + New Features

Trying to play video/stream Fixing Error in QGraphicsVideoItem [SOLVED]

Scheduled Pinned Locked Moved General and Desktop
17 Posts 2 Posters 9.7k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    ion_knight
    wrote on last edited by
    #1

    Can anyone help? Just grabbed this example from the help location of Qt5.1 however the code doesn't compile and fails with these errors.

    C:\Qt\Qt5.1.0\5.1.0\msvc2010_opengl\examples\multimediawidgets\videographicsitem\main.cpp:50: error: C2065: 'player' : undeclared identifier

    C:\Qt\Qt5.1.0\5.1.0\msvc2010_opengl\examples\multimediawidgets\videographicsitem\main.cpp:54: error: C2065: 'graphicsView' : undeclared identifier

    These are the two reason it fails however i have no idea what class i should set these to. For example tried setting player to the class VideoPlayer which it didn't like but i have no idea for graphicsView variable. Anyone got any ideas ?

    Main.cpp

    @

    #include "videoplayer.h"

    #include <QApplication>
    #include <QGraphicsVideoItem>
    #include <QGraphicsView>

    int main(int argc, char **argv)
    {
    QApplication app(argc, argv);
    player = new QMediaPlayer;

    QGraphicsVideoItem *item = new QGraphicsVideoItem;
    player->setVideoOutput(item);
    graphicsView->scene()->addItem(item);
    graphicsView->show();
    
    player->setMedia(QUrl("rtsp://localhost:8554/stream"));
    player->play();
    VideoPlayer player;
    player.show();
    
    return app.exec&#40;&#41;; 
    

    }@

    Videoplayer.cpp-
    @
    #include "videoplayer.h"
    VideoPlayer::VideoPlayer(QWidget *parent)
    : QWidget(parent)
    , mediaPlayer(0, QMediaPlayer::VideoSurface)
    , videoItem(0)
    , playButton(0)
    , positionSlider(0)
    {
    videoItem = new QGraphicsVideoItem;
    videoItem->setSize(QSizeF(640, 480));

    QGraphicsScene *scene = new QGraphicsScene(this);
    QGraphicsView *graphicsView = new QGraphicsView(scene);
    
    scene->addItem(videoItem);
    
    QSlider *rotateSlider = new QSlider(Qt::Horizontal);
    rotateSlider->setRange(-180,  180);
    rotateSlider->setValue(0);
    
    connect(rotateSlider, SIGNAL(valueChanged(int)),
            this, SLOT(rotateVideo(int)));
    
    QAbstractButton *openButton = new QPushButton(tr("Open..."));
    connect(openButton, SIGNAL(clicked()), this, SLOT(openFile&#40;&#41;&#41;&#41;;
    
    playButton = new QPushButton;
    playButton->setEnabled(false);
    playButton->setIcon(style()->standardIcon(QStyle::SP_MediaPlay));
    
    connect(playButton, SIGNAL(clicked()),
            this, SLOT(play()));
    
    positionSlider = new QSlider(Qt::Horizontal);
    positionSlider->setRange(0, 0);
    
    connect(positionSlider, SIGNAL(sliderMoved(int)),
            this, SLOT(setPosition(int)));
    
    QBoxLayout *controlLayout = new QHBoxLayout;
    controlLayout->setMargin(0);
    controlLayout->addWidget(openButton);
    controlLayout->addWidget(playButton);
    controlLayout->addWidget(positionSlider);
    
    QBoxLayout *layout = new QVBoxLayout;
    layout->addWidget(graphicsView);
    layout->addWidget(rotateSlider);
    layout->addLayout(controlLayout);
    
    setLayout(layout);
    
    mediaPlayer.setVideoOutput(videoItem);
    connect(&mediaPlayer, SIGNAL(stateChanged(QMediaPlayer::State)),
            this, SLOT(mediaStateChanged(QMediaPlayer::State)));
    connect(&mediaPlayer, SIGNAL(positionChanged(qint64)), this, SLOT(positionChanged(qint64)));
    connect(&mediaPlayer, SIGNAL(durationChanged(qint64)), this, SLOT(durationChanged(qint64)));
    

    }

    VideoPlayer::~VideoPlayer()
    {
    }

    void VideoPlayer::openFile()
    {
    //QString fileName = QFileDialog::getOpenFileName(this, tr("Open Movie"),QDir::homePath());
    QString fileName = "rtsp://192.168.100.58:8554/stream";
    if (!fileName.isEmpty()) {
    mediaPlayer.setMedia(QUrl::fromLocalFile(fileName));

        playButton->setEnabled(true&#41;;
    }
    

    }

    void VideoPlayer::play()
    {
    switch(mediaPlayer.state()) {
    case QMediaPlayer::PlayingState:
    mediaPlayer.pause();
    break;
    default:
    mediaPlayer.play();
    break;
    }
    }
    @
    videoplayer.h
    @
    #ifndef VIDEOPLAYER_H
    #define VIDEOPLAYER_H

    #include <QMediaPlayer>
    #include <QMovie>
    #include <QWidget>
    #include <QtWidgets>
    #include <QVideoSurfaceFormat>
    #include <QGraphicsVideoItem>

    QT_BEGIN_NAMESPACE
    class QAbstractButton;
    class QSlider;
    class QGraphicsVideoItem;
    QT_END_NAMESPACE

    class VideoPlayer : public QWidget
    {
    Q_OBJECT

    public:
    VideoPlayer(QWidget *parent = 0);
    ~VideoPlayer();

    QSize sizeHint() const { return QSize(800, 600); }
    

    public slots:
    void openFile();
    void play();

    private slots:
    void mediaStateChanged(QMediaPlayer::State state);
    void positionChanged(qint64 position);
    void durationChanged(qint64 duration);
    void setPosition(int position);
    void rotateVideo(int angle);

    private:
    QMediaPlayer mediaPlayer;
    QGraphicsVideoItem *videoItem;
    QAbstractButton *playButton;
    QSlider *positionSlider;
    };

    #endif

    @
    .pro
    @TEMPLATE = app
    TARGET = videographicsitem

    QT += multimedia multimediawidgets

    HEADERS += videoplayer.h

    SOURCES += main.cpp
    videoplayer.cpp

    target.path = $$[QT_INSTALL_EXAMPLES]/multimediawidgets/videographicsitem
    INSTALLS += target

    QT+=widgets
    @

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Line 10: You didn't give the type of player
      Line 14: You didn't declare nor instantiate an object of class QGraphicsView named graphicsView

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • I Offline
        I Offline
        ion_knight
        wrote on last edited by
        #3

        Like I said SGaist this is from the examples on Qt, as said before on line 10 have tried

        VideoPlayer *player = new QMediaPlayer
        got
        C:\Qt\Qt5.1.0\5.1.0\msvc2010_opengl\examples\multimediawidgets\videographicsitem\main.cpp:50: error: C2440: 'initializing' : cannot convert from 'QMediaPlayer *' to 'VideoPlayer'
        No constructor could take the source type, or constructor overload resolution was ambiguous
        tried
        QMediaPlayer *player =new QMediaPlayer
        got
        C:\Qt\Qt5.1.0\5.1.0\msvc2010_opengl\examples\multimediawidgets\videographicsitem\main.cpp:53: error: C2819: type 'QMediaPlayer' does not have an overloaded member 'operator ->'

        neither worked

        also tried
        QGraphicsView graphicsView = new QGraphicsView;

        All I want to do is test this example but have no idea what is missing does seem pretty rubbish that even the examples that Qt provide don't even compile.

        Well found out what the problem was the example under the help section of Qt5.1 isn't correct googled the same page and it has different code tried the code listed on the site instead and it works.

        http://qt-project.org/doc/qt-5.0/qtmultimedia/multimediawidgets-videographicsitem.html

        However if anyone knows how to fix this code please let me know as the example above is alot easier to understand than the link above where i got the working code (using just a normal video file instead of an rtsp stream of course).

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @VideoPlayer *player = new QMediaPlayer; << VideoPlayer is a not the same as a QMediaPlayer@

          Anyway, just keep the main from the example since you already modified openFile.

          Or add a new function to MediaPlayer to open your network media.

          Also note that
          @
          QString fileName = "rtsp://192.168.100.58:8554/stream";
          if (!fileName.isEmpty()) {
          mediaPlayer.setMedia(QUrl::fromLocalFile(fileName));

              playButton-&gt;setEnabled(true);
          }
          

          @

          won't work as expected, you are not giving a local file name there

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • I Offline
            I Offline
            ion_knight
            wrote on last edited by
            #5

            Yea don't really care about the rtsp section until i can get a normal video playing and get the code compiling. So using the default atm

            QString fileName = QFileDialog::getOpenFileName(this, tr("Open Movie"),QDir::homePath());

            So should line 10 be

            QMediaPlayer *player = new QMediaPlayer;?

            As already tried that to no luck , (ha but just tried that again found a typing error gotta love it).

            So after fixing that this is now my current main.

            @#include "videoplayer.h"

            #include <QApplication>
            #include <QGraphicsView>

            int main(int argc, char **argv)
            {
            QApplication app(argc, argv);
            //VideoPlayer player;
            //QGraphicsView graphicsView;
            QMediaPlayer *player = new QMediaPlayer;

            QGraphicsVideoItem *item;
            QGraphicsView *graphicsView;
            player->setVideoOutput(item);
            graphicsView->scene()->addItem(item);
            graphicsView->show();
            
            player->setMedia(QUrl("http://example.com/myclip4.ogv"));
            player->play();
            
            player->show();
            
            return app.exec();
            

            }@

            but gettings these errors.

            C:\Qt\Qt5.1.0\5.1.0\msvc2010_opengl\examples\multimediawidgets\videographicsitem\main.cpp:56: error: C2664: 'QGraphicsScene::addItem' : cannot convert parameter 1 from 'QGraphicsVideoItem *' to 'QGraphicsItem *'
            Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

            C:\Qt\Qt5.1.0\5.1.0\msvc2010_opengl\examples\multimediawidgets\videographicsitem\main.cpp:62: error: C2039: 'show' : is not a member of 'QMediaPlayer'

            Think i understand how to fix the second error by changing player to graphicsView

            e.g.

            graphicsView->show();

            instead of

            player->show();

            am i correct in thinking that ?

            But with the first error not really sure how to proceed, as no idea how you would typecast
            'QGraphicsVideoItem *' to 'QGraphicsItem *'

            is it something like

            addItem(QGraphicsVideoItem(*item);?

            If anyone has a link of how to do this type of typecasting that would be great.

            1 Reply Last reply
            0
            • I Offline
              I Offline
              ion_knight
              wrote on last edited by
              #6

              Ok broke down my player and think I have the most basic QMediaPlayer possible. The audio of this file plays from the console but the video doesn't the file type is .mov (h.264). Is there something that I am missing ?

              main.cpp
              @#include <QCoreApplication>
              #include <QMediaPlayer>
              #include <QFileInfo>

              int main(int argc, char *argv[])
              {
              QCoreApplication a(argc, argv);
              QMediaPlayer media;
              const QString file = "C:/Users/nick/Downloads/big_buck_bunny_1080p_h264.mov";
              QUrl url(QFileInfo(file).absoluteFilePath());
              media.setMedia(url);
              media.play();
              return a.exec();
              }@

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                You're creating a QCoreApplication, which means no GUI. Create QApplication and you should be good.

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • I Offline
                  I Offline
                  ion_knight
                  wrote on last edited by
                  #8

                  Well i think I am getting there (but changing it to a GUI application doesn't seemed to of changed anything) but I'm presuming by pushing the file into a surface now it should play ? (windows error below related to not having a surface set up for the media file ?).

                  However the linux version just looks like i need to have the correct URI path (or do i need the URI and to push it to a surface ?). How should i be defining a path in linux to a file like "file://home/ion/Downloads/big_buck_bunny.mov"? Cause at the moment all i get is the errors listed below.

                  Cheers for help so far.

                  Windows Error:

                  • no videoWindowControl or videoRendererControl, unable to add output node for video data

                  Linux Errors:

                  • GStreamer; Unable to pause - "/home/ion/Qt_practice/build-test-Desktop_Qt_5_1_0_GCC_64bit-Debug/big_buck_bunny_480p_h264.mov"

                  • GStreamer; Unable to play - "/home/ion/Qt_practice/build-test-Desktop_Qt_5_1_0_GCC_64bit-Debug/big_buck_bunny_480p_h264.mov"

                  • Error: "Invalid URI "/home/ion/Qt_practice/build-test-Desktop_Qt_5_1_0_GCC_64bit-Debug/big_buck_bunny_480p_h264.mov"."

                  @#include "mainwindow.h"
                  #include <QApplication>
                  #include <QMediaPlayer>
                  #include <QFileInfo>
                  #include <QDebug>

                  int main(int argc, char *argv[])
                  {
                  QApplication a(argc, argv);
                  MainWindow w;
                  QMediaPlayer media;
                  const QString file = "big_buck_bunny_480p_h264.mov";
                  QUrl url(QFileInfo(file).absoluteFilePath());
                  qDebug() << url << endl;
                  media.setMedia(url);
                  media.play();
                  w.show();

                  return a.exec&#40;&#41;;
                  

                  }
                  @

                  Ha well i think I am getting closer hopefully this should be the last post :P.

                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    Do you have the same error using

                    @QUrl url = QUrl::fromLocalFile(QFileInfo(file).absoluteFilePath());@

                    ?

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    0
                    • I Offline
                      I Offline
                      ion_knight
                      wrote on last edited by
                      #10

                      Lol well that sorted that linux problem feel a bit foolish now (note to self don't watch a video when it's in a foreign language just asking for trouble) . Now both the linux and the Windows version are doing the exact same only difference is the error output.

                      Both playing audio but no video. Do i need a Abstract Video surface or ?
                      Linux:

                      OpenGL Warning: XGetVisualInfo returned 0 visuals for 0x1b800a0

                      OpenGL Warning: Retry with 0x8002 returned 0 visuals

                      OpenGL Warning: XGetVisualInfo returned 0 visuals for 0x1b800a0

                      OpenGL Warning: Retry with 0x8002 returned 0 visuals

                      OpenGL Warning: glXGetFBConfigAttrib for 0x1b800a0, failed to get XVisualInfo

                      over and over

                      Windows:

                      no videoWindowControl or videoRendererControl, unable to add output node for video data

                      but yea still get the same error on the windows side. Also just so your aware running the linux machine in a VM.

                      1 Reply Last reply
                      0
                      • SGaistS Offline
                        SGaistS Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        A QVideoWidget should do it

                        Interested in AI ? www.idiap.ch
                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                        1 Reply Last reply
                        0
                        • I Offline
                          I Offline
                          ion_knight
                          wrote on last edited by
                          #12

                          Ha yea What i ended up using in the end below is the updated code using QGraphicsView or QVideoWidget. Ha now all i need to do is figure out how to apply this to an RTSP stream but thats for another day :D. Cheers SGaist been very helpful.

                          @#include "mainwindow.h"
                          #include <QApplication>
                          #include <QMediaPlayer>
                          #include <QFileInfo>
                          #include <QDebug>
                          #include <QVideoWidget>
                          #include <QGraphicsView>
                          #include <QGraphicsVideoItem>

                          int main(int argc, char *argv[])
                          {
                          QApplication a(argc, argv);
                          MainWindow w;

                          QMediaPlayer *player = new QMediaPlayer;
                           player->setMedia(QUrl::fromLocalFile&#40;"C:/Users/nick/Downloads/big_buck_bunny_1080p_h264.mov"&#41;&#41;;
                           QVideoWidget *videoWidget = new QVideoWidget;
                           w.setCentralWidget(videoWidget&#41;; // if w is a QMainWindow
                           player->setVideoOutput(videoWidget);
                           player->play();
                          

                          /*
                          QGraphicsView *graphicsView = new QGraphicsView;
                          w.setCentralWidget(graphicsView); // w = QMainWindow
                          QGraphicsScene *scene = new QGraphicsScene;
                          QMediaPlayer *player = new QMediaPlayer;
                          QGraphicsVideoItem *item = new QGraphicsVideoItem;

                          graphicsView->setScene(scene);
                          player->setVideoOutput(item);
                          
                          graphicsView->scene()->addItem(item);
                          //player->setMedia(QUrl::fromLocalFile&#40;"C:/Users/nick/Downloads/big_buck_bunny_1080p_h264.mov"&#41;&#41;;
                          player->setMedia(QUrl("rtsp://localhost:8554/stream"&#41;);
                          player->play();*/
                          
                          w.show();
                          
                          return a.exec(&#41;;
                          

                          }@

                          Btw when should I be using either? because currently just tempted to do all video output using videowidget as it looks the simpliest, is there any situation when i should use Graphics View or QAbstractVideoSurface over VideoWidget?

                          1 Reply Last reply
                          0
                          • SGaistS Offline
                            SGaistS Offline
                            SGaist
                            Lifetime Qt Champion
                            wrote on last edited by
                            #13

                            For your tests, you should keep using the QVideoWidget. Once you have everything working, you can start playing with QGraphicsView and friends

                            Interested in AI ? www.idiap.ch
                            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                            1 Reply Last reply
                            0
                            • I Offline
                              I Offline
                              ion_knight
                              wrote on last edited by
                              #14

                              Doesn't really answer my question but ah well setting it to solved.

                              1 Reply Last reply
                              0
                              • SGaistS Offline
                                SGaistS Offline
                                SGaist
                                Lifetime Qt Champion
                                wrote on last edited by
                                #15

                                Did you meant: In which case should I use QVideoWidget and in which one QGraphicsView ?

                                Interested in AI ? www.idiap.ch
                                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                1 Reply Last reply
                                0
                                • I Offline
                                  I Offline
                                  ion_knight
                                  wrote on last edited by
                                  #16

                                  Yea can't seemed to find a good example of their usage. Also would you know why QVideoWidget doesn't work on linux with that code but graphicsview does?

                                  @#include "mainwindow.h"
                                  #include <QApplication>
                                  #include <QMediaPlayer>
                                  #include <QFileInfo>
                                  #include <QDebug>
                                  #include <QVideoWidget>
                                  #include <QGraphicsView>
                                  #include <QGraphicsVideoItem>

                                  int main(int argc, char argv[])
                                  {
                                  QApplication a(argc, argv);
                                  MainWindow w;
                                  /
                                  QMediaPlayer *player = new QMediaPlayer;
                                  const QString file = "big_buck_bunny_480p_h264.mov";
                                  QUrl url = QUrl::fromLocalFile(QFileInfo(file).absoluteFilePath());
                                  player->setMedia(url);
                                  QVideoWidget videoWidget = new QVideoWidget;
                                  w.setCentralWidget(videoWidget); // if w is a QMainWindow
                                  player->setVideoOutput(videoWidget);
                                  player->play();
                                  /

                                   QGraphicsView *graphicsView = new QGraphicsView;
                                   w.setCentralWidget(graphicsView); // w = QMainWindow
                                   QGraphicsScene *scene       = new QGraphicsScene;
                                   QMediaPlayer *player        = new QMediaPlayer;
                                   QGraphicsVideoItem *item    = new QGraphicsVideoItem;
                                  
                                   graphicsView->setScene(scene);
                                   player->setVideoOutput(item);
                                   const QString file = "big_buck_bunny_480p_h264.mov";
                                   QUrl url = QUrl::fromLocalFile&#40;QFileInfo(file&#41;.absoluteFilePath(&#41;&#41;;
                                   graphicsView->scene(&#41;->addItem(item);
                                   player->setMedia(url);
                                   //player->setMedia(QUrl("rtsp://localhost:8554/stream"));
                                   player->play();
                                        qDebug() << url << endl;
                                  w.show();
                                  return a.exec&#40;&#41;;
                                  

                                  }
                                  @

                                  The weird thing is linux says that it "Failed to start video surface" yet the video still plays bit random (but crashes after a while sometimes). (mind you probably a good idea to just create a new post to answer this question).

                                  1 Reply Last reply
                                  0
                                  • SGaistS Offline
                                    SGaistS Offline
                                    SGaist
                                    Lifetime Qt Champion
                                    wrote on last edited by
                                    #17

                                    The only good answer is: it really depends on the application you are writing (I know might not sound helpful) If you are not doing anything special that would require a QGraphicsView then go for the widget.

                                    As for your last question, you could directly try to see the "bug report system":http://bugreports.qt-project.org/issues and check whether it's a known issue

                                    Interested in AI ? www.idiap.ch
                                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                    1 Reply Last reply
                                    0

                                    • Login

                                    • Login or register to search.
                                    • First post
                                      Last post
                                    0
                                    • Categories
                                    • Recent
                                    • Tags
                                    • Popular
                                    • Users
                                    • Groups
                                    • Search
                                    • Get Qt Extensions
                                    • Unsolved