0

I have been using QGraphicsScene and QGraphicsVideoItem as my canvas. And to control them, I've chosen to use qml and QQuickWidget to develop custom objects easily for a different module. However, I quickly ran into an issue where the QGraphicsVideoItem would not render in the QGraphicsScene but rather inside the QQuickWidget (both when the widget is empty or with a qml source attached). And the issue seems to be reproducible using a fresh project as well just by placing an empty QQuickWidget(through qt designer) anywhere inside the main ui.

Here is the reproducible code:

#include "QtGuiApplication1.h"

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

    QGraphicsView* view = new QGraphicsView(ui.widget);
    QGraphicsScene* scene = new QGraphicsScene();
    QGraphicsVideoItem* video = new QGraphicsVideoItem();
    QMediaPlayer* player = new QMediaPlayer();
    QUrl path = QUrl::fromLocalFile("D:/My Documents/Videos/XIII.mp4");
    QVBoxLayout* layout = new QVBoxLayout();

    layout->addWidget(view);
    ui.widget->setLayout(layout);
    video->setFlags(QGraphicsVideoItem::ItemIsMovable | QGraphicsVideoItem::ItemIsFocusable | QGraphicsVideoItem::ItemIsSelectable);
    video->setPos(100, 100);

    //view->setSceneRect(QRectF(QPointF(100, 100), QPointF(800, 600)));
    view->setScene(scene);

    player->setMedia(path);
    player->setVideoOutput(video);
    scene->addItem(video);

    player->play();

    view->show();

}
#pragma once

#include "ui_QtGuiApplication1.h"
#include <QtCore>
#include <QDebug>
#include <QGraphicsVideoItem>
#include <QGraphicsView>
#include <QGraphicsScene>
#include <QMediaPlayer>
#include <QUrl>
#include <QString>

class QtGuiApplication1 : public QMainWindow
{
    Q_OBJECT

public:
    QtGuiApplication1(QWidget *parent = Q_NULLPTR);

private:
    Ui::QtGuiApplication1Class ui;

};

The issue immediately went away when I removed the widget from the ui file as well. So am I missing something here?

Mervyn
  • 11
  • 3

1 Answers1

0

When you call ui.widget->setLayout(layout); you break the layout that was set in Qt Designer.

Instead of programmatically creating the QGraphicsView and QVBoxLayout in your *.cpp file, add them all in Qt Designer.

(If the issue still persists, please edit your original post and include your *.ui file)

JKSH
  • 2,658
  • 15
  • 33