c++qtqt5qlayoutqvideowidget

QVideoWidget doesn't show anything when aligned in a layout


I have a QMediaPlayer, QVideoWidget and some layout (ex. QVboxLayout).

The problem is when I add QVideoWidget with alignment specified — the video doesn't show up, there's only a place for the widget, a blank area with no video. And there's no problem if I add it without specifying alignment.

Here's the code example:

Widget.h:

class Widget : public QWidget
{
  Q_OBJECT

public:
  Widget(QWidget *parent = 0)
    : QWidget(parent)
  {
    QVideoWidget *vw = new QVideoWidget();
    QVBoxLayout *vb = new QVBoxLayout();
    vb->addWidget(vw, 0/*, Qt::AlignCenter*/); /// << here's the problem block
    this->setLayout(vb);

    QMediaPlayer *pl = new QMediaPlayer();
    pl->setMedia(QMediaContent(
                   QUrl::fromLocalFile("D:/1.wmv")));
    pl->setVideoOutput(vw);
    pl->play();
    this->resize(600, 480);
  }
  ~Widget() {}
};

If "qDebugging" geometry() of the video widget with center alignment "off" it looks like this:

QRect(11,11 579x459)

But if I try to align it to the center it looks like this:

QRect(300,240 0x0)
AlignLeft: *QRect(11,11 0x456)

It repeats even if I change QVideoWidget with QGraphicsScene, QGraphicsView, or QGraphicsVideoItem chain.


Solution

  • I've found the advice not to use alignment on widgets in layout, instead it's better to use alignment inside the widgets that have to be aligned.

    That's why I've turned layout's alignment off, and centered the content of the widgets, that are inserted into the layout.

    Looks like a hack, but it really works.