Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Solved GraphicsView crash with GraphicsVideoItem.

    General and Desktop
    mediaplayer graphicview qgraphicsvideoi form
    2
    3
    1093
    Loading More Posts
    • 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.
    • Fidchells_Eye
      Fidchells_Eye last edited by

      Hi
      I've been trying my hand at building a simple media player with the form for object placement.
      But it crashes just as it starts to run.


      Constructor Code:
      ui->setupUi(this);
      videoItem = new QGraphicsVideoItem();

      ui->GVpreview->scene()->addItem(videoItem); // << --- Trouble maker that causes crash
      ui->GVpreview->show(); // GVpreview = QGraphicsView

      connect(ui->hsdrDuration, SIGNAL(sliderMoved(int)), this, SLOT(setPosition(int)));
      mediaPlayer.setVideoOutput(videoItem);
      connect(&mediaPlayer, SIGNAL(stateChanged(QMediaPlayer::State)),this, SLOT(mediaStateChanged(QMediaPlayer::State)));


      For some reason the crash happens at ui->GVpreview->scene()->addItem(videoItem); and don't know why or how to fix it.

      Thank you for your replies and time.

      Joel Bodenmann 1 Reply Last reply Reply Quote 0
      • Joel Bodenmann
        Joel Bodenmann @Fidchells_Eye last edited by Joel Bodenmann

        Is it possible that you forgot to create and/or assign the scene to the view?
        Unless you can do that in the QtDesigner you have to do something like this:

        // This creates the QGraphicsView, but there's still no QGraphicsScene!
        ui->setupUi(this);
        
        // Create and assign the QGraphicsScene
        QGraphicsScene* scene = new QGraphicsScene(this);
        ui->GVpreview->setScene(scene);
        
        // Fun can start here
        videoItem = new QGraphicsVideoItem();
        ui->GVpreview->scene()->addItem(videoItem);
        

        In general you have to check the pointer returned by QGraphicsView::scene() before accessing/using it. Most likely your app crashes because you get a nullpointer back.

        Industrial process automation software: https://simulton.com
        Embedded Graphics & GUI library: https://ugfx.io

        1 Reply Last reply Reply Quote 1
        • Fidchells_Eye
          Fidchells_Eye last edited by

          Hey Joel

          Thanks the code lines:
          QGraphicsScene* scene = new QGraphicsScene(this);
          ui->GVpreview->setScene(scene);

          fixed the issue as I don't know how to do the scene view bit in the form editor.

          1 Reply Last reply Reply Quote 0
          • First post
            Last post