0

我想在全屏上添加一些控件和信息QVideoWidget。因此,我创建了一个QMainWindowui,然后将中央小部件提升QVideoWidget为: 在此处输入图像描述

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>VideoScreen</class>
 <widget class="QMainWindow" name="VideoScreen">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>800</width>
    <height>600</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <property name="styleSheet">
   <string notr="true"/>
  </property>
  <widget class="QVideoWidget" name="videoScreen">
   <property name="styleSheet">
    <string notr="true"/>
   </property>
  </widget>
 </widget>
 <customwidgets>
  <customwidget>
   <class>QVideoWidget</class>
   <extends>QWidget</extends>
   <header>QVideoWidget</header>
   <container>1</container>
  </customwidget>
 </customwidgets>
 <resources/>
 <connections/>
</ui>

ControlBoardsui: 在此处输入图像描述

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>ControlBoard</class>
 <widget class="QWidget" name="ControlBoard">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>1044</width>
    <height>520</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Form</string>
  </property>
  <property name="styleSheet">
   <string notr="true"/>
  </property>
  <widget class="QLabel" name="title">
   <property name="geometry">
    <rect>
     <x>210</x>
     <y>90</y>
     <width>59</width>
     <height>17</height>
    </rect>
   </property>
   <property name="styleSheet">
    <string notr="true">
</string>
   </property>
   <property name="text">
    <string>Title</string>
   </property>
  </widget>
  <widget class="QPushButton" name="play">
   <property name="geometry">
    <rect>
     <x>320</x>
     <y>120</y>
     <width>101</width>
     <height>31</height>
    </rect>
   </property>
   <property name="styleSheet">
    <string notr="true"/>
   </property>
   <property name="text">
    <string>play</string>
   </property>
  </widget>
  <widget class="QPushButton" name="stop">
   <property name="geometry">
    <rect>
     <x>210</x>
     <y>120</y>
     <width>101</width>
     <height>31</height>
    </rect>
   </property>
   <property name="styleSheet">
    <string notr="true"/>
   </property>
   <property name="text">
    <string>stop</string>
   </property>
  </widget>
 </widget>
 <resources/>
 <connections/>
</ui>

然后在 中videoscreen.cpp,我还添加了ControlBoardas 兄弟QVideoWidget

VideoScreen::VideoScreen(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::VideoScreen), player(new QMediaPlayer)
{

    ui->setupUi(this);

    player->setVideoOutput(ui->videoScreen);
    ui->videoScreen->lower();
    ControlBoard *cb = new ControlBoard(this);
}

当我播放视频时,结果: 在此处输入图像描述

如何使ControlBoard透明,以便我们可以看到后面的视频?
我在构造函数中尝试了一些代码ControlBoard

//    setWindowFlags(Qt::Widget | Qt::FramelessWindowHint);
//    setAttribute(Qt::WA_NoSystemBackground);
//    setAttribute(Qt::WA_TranslucentBackground);
//    setAttribute(Qt::WA_PaintOnScreen);
//    setAttribute(Qt::WA_TransparentForMouseEvents);
//    setAutoFillBackground(false);

但这只是显示父母的背景,而不是兄弟视频。Qt5 支持这个吗?谢谢!

更新
我也尝试过其他类似的东西(基于):

class OverlayWidget : public QWidget
{
   void newParent() {
      if (!parent()) return;
      parent()->installEventFilter(this);
      raise();
   }
public:
   explicit OverlayWidget(QWidget * parent = {}) : QWidget{parent} {
      setAttribute(Qt::WA_NoSystemBackground);
      setAttribute(Qt::WA_TransparentForMouseEvents);
      newParent();
   }
protected:
   //! Catches resize and child events from the parent widget
   bool eventFilter(QObject * obj, QEvent * ev) override {
      if (obj == parent()) {
         if (ev->type() == QEvent::Resize)
         {
//            resize(static_cast<QResizeEvent*>(ev)->size());
         }
         else if (ev->type() == QEvent::ChildAdded)
            raise();
      }
      return QWidget::eventFilter(obj, ev);
   }
   //! Tracks parent widget changes
   bool event(QEvent* ev) override {
      if (ev->type() == QEvent::ParentAboutToChange) {
         if (parent()) parent()->removeEventFilter(this);
      }
      else if (ev->type() == QEvent::ParentChange)
         newParent();
      return QWidget::event(ev);
   }
};

class LoadingOverlay : public OverlayWidget
{
public:
   LoadingOverlay(QWidget * parent = {}) : OverlayWidget{parent} {
      setAttribute(Qt::WA_TranslucentBackground);
   }
protected:
   void paintEvent(QPaintEvent *) override {
      QPainter p(this);
//      p.fillRect(rect(), {10, 10, 10, 100});
      p.setPen({200, 200, 255});
      p.setFont({"arial,helvetica", 48});
      p.drawText(rect(), "Loading...", Qt::AlignHCenter | Qt::AlignVCenter);
   }
};

int main(int argc, char * argv[])
{
   QApplication a(argc, argv);

   QMainWindow window;
   QVideoWidget central(&window);
   central.setMinimumSize(600, 700);
   window.setCentralWidget(&central);

   QMediaPlayer player;
   player.setMedia(QUrl::fromLocalFile("dolbycanyon.mov"));
   player.setVideoOutput(&central);

   LoadingOverlay overlay(&window);
   overlay.resize(400, 300);
   overlay.setWindowOpacity(.4);
   overlay.setWindowFlags(Qt::Widget | Qt::FramelessWindowHint);
   overlay.setAttribute(Qt::WA_PaintOnScreen);
   overlay.setAutoFillBackground(false);
//   QTimer::singleShot(5000, &overlay, SLOT(hide()));
//   QTimer::singleShot(7000, &player, SLOT(play()));

   player.play();
   window.show();

   return a.exec();
}

但结果仍然是不透明的,不透明的:
在此处输入图像描述

4

1 回答 1

3

视频上透明背景叠加的演示:

#include <QApplication>
#include <QGraphicsView>
#include <QGraphicsScene>
#include <QGraphicsVideoItem>
#include <QMediaPlayer>
#include <QFileDialog>
#include <QPushButton>
#include <QLabel>
#include <QGraphicsProxyWidget>

const QSizeF VideoItemSize(500, 500);

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QMediaPlayer player;
    QGraphicsView v;
    QGraphicsScene scene;
    QGraphicsVideoItem video;
    v.setScene(&scene);
    video.setSize(VideoItemSize);
    scene.setSceneRect(QRectF(QPointF(0, 0), VideoItemSize)); // VideoItem full fill the scene
    scene.addItem(&video);
    player.setVideoOutput(&video);
    player.setMedia(QMediaContent(QFileDialog::getOpenFileUrl()));

    // Recommend using QGraphicsItems for overlay component
    QGraphicsTextItem text("Loading...",&video);
    text.setPos(100, 150);

    // If you need a button...
    QPushButton button("ButtonTest");
    QGraphicsProxyWidget* proxyButton = scene.addWidget(&button);
    proxyButton->setPos(100, 200);

    // Instead of QGraphicsItems, if you really need a QWidget...
    QLabel label("LabelTest");
    label.setAttribute(Qt::WA_TranslucentBackground); // You can delete this line to see different
    QGraphicsProxyWidget* proxyLabel = scene.addWidget(&label);
    proxyLabel->setPos(100, 250);

    v.show();
    player.play();

    return a.exec();
}
于 2018-10-02T05:15:42.127 回答