简体   繁体   中英

QVideoWidget doesn't show anything when aligned

I've got a strange problem and need some help. The situation is quiet easy: I have a QMediaPlayer , QVideoWidget and some layout (ex. QVboxLayout ; well, it doesn't really matter). The problem is when I add QVideoWidget with alignment specified - 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() {}
};

Any suggestions? Thanks in advance.

UPD:

Found an interesting thing - if "qDebugging" geometry() of 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)

Don't know why this happening and what is the right way to resolve this issue, but it repeats even if I change QVideoWidget with QGraphicsScene-QGraphicsView-QGraphicsVideoItem chain.

Well, here on stackoverflow (sorry, missed the exactly link) 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. Thanks for the help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM