锐单电子商城 , 一站式电子元器件采购平台!
  • 电话:400-990-0325

QWebEngineView 加载视频和最大化视频窗口

时间:2023-10-18 21:37:00 ad7819yrz集成电路ic

加载网页视频

  • QT 下载的QWebEngineView没有办法加载视频。如果需要加载视频,则需要重新编译相应的视频dll,我已经在这里编译并直接提供下载连接

QT5.15.2

链接:https://pan.baidu.com/s/1Fmzj7W8P9W9a3yrz6yN_Gw  提取码:1111  --百度网盘超级会员V6的分享 如果你的版本和我的一致,你可以直接使用我的版本 

加载完成后不能最大化,请参考以下代码

因为搜索相关文章发现文章很少(确切地说,我没有搜索,也许我不太擅长搜索),所以写这篇文章作为记录
代码没有解释,整体逻辑比较简单。如果需要,我可以发私信,我看到了一些回复
EmbeddedWeb.h

 #ifndef EDI_EMBEDDEDWEB_H #define EDI_EMBEDDEDWEB_H  #include  #include  #include  #include  #include "ui_EmbeddedWeb.h"  QT_BEGIN_NAMESPACE namespace Ui { 
         class EmbeddedWeb; } QT_END_NAMESPACE  class WebFullScreenWindow;  class EmbeddedWeb : public QWidget { 
             Q_OBJECT public:     explicit EmbeddedWeb(QWidget *parent = nullptr,QString url="");      ~EmbeddedWeb() override;      void initUi();      void setting();  protected:     void showEvent(QShowEvent *e) override;  private slots:     void loadPageFinished(bool ok);     void fullScreenRequested(QWebEngineFullScreenRequest request);

private:
    Ui::EmbeddedWeb ui{ 
        };
    QString url_{ 
        };
    QWebChannel *channel_{ 
        nullptr};
    bool load_finished_{ 
        false};
    QTimer timer;
    QScopedPointer<WebFullScreenWindow> m_fullScreenWindow;
};


#endif //EDI_EMBEDDEDWEB_H

EmbeddedWeb.cpp

#include "EmbeddedWeb.h"
#include "WebFullScreenWindow.h"
#include 
#include 
#include 
#include 
#include 
#include 
#include 

EmbeddedWeb::EmbeddedWeb(QWidget *parent,QString url) :
        QWidget(parent)
{ 
        
    ui.setupUi(this);
    QNetworkProxyFactory::setUseSystemConfiguration(false);
    url_ = url;
    initUi();
    setting();
}

EmbeddedWeb::~EmbeddedWeb()
{ 
        

}

void EmbeddedWeb::initUi()
{ 
        
    channel_ = new QWebChannel(this);
    ui.webEngineView->page()->setWebChannel(channel_);
    ui.webEngineView->setUrl(QUrl::fromUserInput(url_));
    QWebEngineSettings* setting = ui.webEngineView->settings();
    setting->setAttribute(QWebEngineSettings::FullScreenSupportEnabled, true);
    // fullScreenRequested(QWebEngineFullScreenRequest fullScreenRequest);
}

void EmbeddedWeb::setting()
{ 
        
    connect(ui.webEngineView,&QWebEngineView::loadFinished,this,&EmbeddedWeb::loadPageFinished);
    connect(ui.webEngineView->page(),&QWebEnginePage::fullScreenRequested,this,&EmbeddedWeb::fullScreenRequested);
}

void EmbeddedWeb::loadPageFinished(bool ok)
{ 
        
    if(!ok)
    { 
        
        ui.webEngineView->reload();
    }
}

void EmbeddedWeb::showEvent(QShowEvent *e)
{ 
        
    showMaximized();
}

void EmbeddedWeb::fullScreenRequested(QWebEngineFullScreenRequest request)
{ 
        
    if (request.toggleOn()) { 
        
        if (m_fullScreenWindow)
            return;
        request.accept();
        m_fullScreenWindow.reset(new WebFullScreenWindow(ui.webEngineView));
    } else { 
        
        if (!m_fullScreenWindow)
            return;
        request.accept();
        m_fullScreenWindow.reset();
    }

WebFullScreenWindow.h


#ifndef EDI_WEBFULLSCREENWINDOW_H
#define EDI_WEBFULLSCREENWINDOW_H

#include 

QT_BEGIN_NAMESPACE
class QWebEngineView;
QT_END_NAMESPACE

class FullScreenNotification;

class WebFullScreenWindow : public QWidget
{ 
        
    Q_OBJECT
public:
    explicit WebFullScreenWindow(QWebEngineView *oldView, QWidget *parent = nullptr);
    ~WebFullScreenWindow();

protected:
    void resizeEvent(QResizeEvent *event) override;

private:
    QWebEngineView *m_view;
    FullScreenNotification *m_notification;
    QWebEngineView *m_oldView;
    QRect m_oldGeometry;
};

#include 

class FullScreenNotification : public QLabel
{ 
        
Q_OBJECT
public:
    FullScreenNotification(QWidget *parent = nullptr);

protected:
    void showEvent(QShowEvent *event) override;

signals:
    void shown();

private:
    bool m_previouslyVisible;
};


#endif //EDI_WEBFULLSCREENWINDOW_H

WebFullScreenWindow.cpp


#include "WebFullScreenWindow.h"
#include 
#include 

WebFullScreenWindow::WebFullScreenWindow(QWebEngineView *oldView, QWidget *parent)
        : QWidget(parent)
        , m_view(new QWebEngineView(this))
        , m_notification(new FullScreenNotification(this))
        , m_oldView(oldView)
        , m_oldGeometry(oldView->window()->geometry())
{ 
        
    m_view->stackUnder(m_notification);

    auto exitAction = new QAction(this);
    exitAction->setShortcut(Qt::Key_Escape);
    connect(exitAction, &QAction::triggered, [this]() { 
        
        m_view->triggerPageAction(QWebEnginePage::ExitFullScreen);
    });
    addAction(exitAction);

    m_view->setPage(m_oldView->page());
    setGeometry(m_oldGeometry);
    showFullScreen();
    m_oldView->window()->hide();
}

WebFullScreenWindow::~WebFullScreenWindow()
{ 
        
    m_oldView->setPage(m_view->page());
    m_oldView->window()->setGeometry(m_oldGeometry);
    m_oldView->window()->show();
    hide();
}

void WebFullScreenWindow::resizeEvent(QResizeEvent *event)
{ 
        
    QRect viewGeometry(QPoint(0, 0), size());
    m_view->setGeometry(viewGeometry);

    QRect notificationGeometry(QPoint(0, 0), m_notification->sizeHint());
    notificationGeometry.moveCenter(viewGeometry.center());
    m_notification->setGeometry(notificationGeometry);

    QWidget::resizeEvent(event);
}

FullScreenNotification::FullScreenNotification(QWidget *parent)
        : QLabel(parent)
        , m_previouslyVisible(false)
{ 
        
    setText(tr("You are now in full screen mode. Press ESC to quit!"));
    setStyleSheet(
            "font-size: 24px;"
            "color: white;"
            "background-color: black;"
            "border-color: white;"
            "border-width: 2px;"
            "border-style: solid;"
            "padding: 100px");

    setAttribute(Qt::WA_TransparentForMouseEvents);

    auto effect = new QGraphicsOpacityEffect;
    effect->setOpacity(1);
    setGraphicsEffect(effect);

    auto animations = new QSequentialAnimationGroup(this);
    animations->addPause(3000);
    auto opacityAnimation = new QPropertyAnimation(effect, "opacity", animations);
    opacityAnimation->setDuration(2000);
    opacityAnimation->setStartValue(1.0);
    opacityAnimation->setEndValue(0.0);
    opacityAnimation->setEasingCurve(QEasingCurve::OutQuad);
    animations->addAnimation(opacityAnimation);

    connect(this, &FullScreenNotification::shown,
            [animations](){ 
         animations->start(); });

    connect(animations, &QAbstractAnimation::finished,
            [this](){ 
         this->hide(); });
}

void FullScreenNotification::showEvent(QShowEvent *event)
{ 
        
    QLabel::showEvent(event);
    if (!m_previouslyVisible && isVisible())
            emit shown();
    m_previouslyVisible = isVisible();
}
锐单商城拥有海量元器件数据手册IC替代型号,打造电子元器件IC百科大全!

相关文章