GraphicsView crash with GraphicsVideoItem.



  • 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.



  • 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.



  • 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.



Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.