Qt 如何在兄弟QVideoWidget之上创建透明QWidget?
我想在全屏Qt 如何在兄弟QVideoWidget之上创建透明QWidget?,qt,qt5,transparent,qvideowidget,Qt,Qt5,Transparent,Qvideowidget,我想在全屏QVideoWidget上添加一些控件和信息。因此,我创建了一个QMainWindowui,然后将中心小部件升级为QVideoWidget,如下所示: 然后在videoscreen.cpp中,我还添加了ControlBoard作为QVideoWidget的兄弟: VideoScreen::VideoScreen(QWidget *parent) : QMainWindow(parent), ui(new Ui::VideoScreen), player(new QMe
QVideoWidget
上添加一些控件和信息。因此,我创建了一个QMainWindow
ui,然后将中心小部件升级为QVideoWidget
,如下所示:
然后在videoscreen.cpp
中,我还添加了ControlBoard
作为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
constructor中尝试了一些代码:
// 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:publicqwidget
{
void newParent(){
如果(!parent())返回;
父级()->installEventFilter(此);
升起();
}
公众:
显式覆盖Widget(QWidget*parent={}):QWidget{parent}{
setAttribute(Qt::WA_NoSystemBackground);
setAttribute(Qt::WA_TransparentForMouseEvents);
newParent();
}
受保护的:
//!从父窗口小部件捕获调整大小和子事件
布尔事件过滤器(QObject*obj、QEvent*ev)覆盖{
如果(obj==parent()){
如果(ev->type()==QEvent::Resize)
{
//调整大小(静态投影(ev)->size());
}
else if(ev->type()==QEvent::ChildAdded)
升起();
}
返回QWidget::eventFilter(obj,ev);
}
//!跟踪父窗口小部件的更改
布尔事件(QEvent*ev)覆盖{
if(ev->type()==QEvent::ParentAboutToChange){
if(parent())parent()->removeEventFilter(此);
}
否则如果(ev->type()==QEvent::ParentChange)
newParent();
返回QWidget::事件(ev);
}
};
类装入Overlay:公共覆盖Widget
{
公众:
LoadingOverlay(QWidget*parent={}):OverlayWidget{parent}{
setAttribute(Qt::WA_半透明背景);
}
受保护的:
无效paintEvent(QPaintEvent*)覆盖{
油漆工p(本);
//p.fillRect(rect(),{10,10,10100});
p、 setPen({200200255});
p、 setFont({“arial,helvetica”,48});
p、 drawText(rect(),“加载…”,Qt::AlignHCenter | Qt::AlignVCenter);
}
};
int main(int argc,char*argv[])
{
质量保证申请a(argc、argv);
qmain窗口;
QVideoWidget中心(窗口和窗口);
中央。设置最小大小(600700);
window.setCentralWidget(¢ral);
QMediaPlayer播放器;
setMedia(QUrl::fromLocalFile(“dolbyconyon.mov”);
player.setVideoOutput(¢ral);
加载覆盖层(和窗口);
覆盖。调整大小(400300);
覆盖.setWindowOpacity(.4);
setWindowFlags(Qt::Widget | Qt::FramelessWindowHint);
setAttribute(Qt::WA_PaintOnScreen);
overlay.setAutoFillBackground(false);
//QTimer::单镜头(5000,&覆盖,插槽(hide());
//QTimer::单发(7000,&播放器,插槽(play());
player.play();
window.show();
返回a.exec();
}
但结果仍然不透明,不透明:视频透明背景叠加演示:
#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();
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
const QSizeF VideoItemSize(500500);
int main(int argc,char*argv[])
{
质量保证申请a(argc、argv);
QMediaPlayer播放器;
QGraphicsView v;
QsCENE场景;
QGraphicsVideoItem视频;
v、 场景(和场景);
video.setSize(VideoItemSize);
scene.setscen直立(QRectF(QPointF(0,0),VideoItemSize));//VideoItem完全填充场景
场景。添加项(和视频);
player.setVideoOutput(&video);
setMedia(QMediaContent(QFileDialog::getOpenFileUrl());
//建议将QGraphicsSitems用于覆盖组件
QGraphicsTextItem文本(“加载…”,&视频);
text.setPos(100150);
//如果你需要一个按钮。。。
QPushButton按钮(“按钮测试”);
QGraphicsProxyWidget*proxyButton=scene.addWidget(&button);
proxyButton->setPos(100200);
//如果你真的需要一个QWidget而不是QGraphicsItems。。。
QLabel标签(“标签测试”);
label.setAttribute(Qt::WA_TranslucentBackground);//您可以删除此行以查看不同的
QGraphicsProxyWidget*proxyLabel=scene.addWidget(&label);
proxyLabel->setPos(100250);
v、 show();
player.play();
返回a.exec();
}
视频透明背景叠加演示:
#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();
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
const QSizeF VideoItemSize(500500);
int main(int argc,char*argv[])
{
质量保证申请a(argc、argv);
QMediaPlayer播放器;
QGraphicsView v;
QsCENE场景;
QGraphicsVideoItem视频;
v、 场景(和场景);
video.setSize(VideoItemSize);
scene.setscen直立(QRectF(QPointF(0,0),VideoItemSize));//VideoItem完全填充场景
场景。添加项(和视频);
player.setVideoOutput(&video);
setMedia(QMediaContent(QFileDialog::getOpenFileUrl());
//建议将QGraphicsSitems用于覆盖组件
QGraphicsTextItem文本(“加载…”,&视频);
text.setPos(100150);
//如果你需要一个按钮。。。
QPushButton按钮(“按钮测试”);
QGraphicsProxyWidget*proxyButton=scene.addWidget(&button);
proxyButton->setPos(100200);
//如果你真的需要一个QWidget而不是QGraphicsItems。。。
QLabel标签(“标签测试”);
label.setAttribute(Qt::WA_TranslucentBackground);//您可以删除此行以查看不同的
QGraphicsProxyWidget*proxyLabel=scene.addWidget(&label);
proxyLabel->setPos(100250);
v、 show();
player.play();
返回a.exec();
}
覆盖组件是什么?“加载”文本?它们是任何小部件:按钮、标签等。我想在顶部的覆盖层上添加一些控件和信息。请注意,除了其他内容外,您似乎正在添加控制板
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(¢ral);
QMediaPlayer player;
player.setMedia(QUrl::fromLocalFile("dolbycanyon.mov"));
player.setVideoOutput(¢ral);
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();
}
#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();
}