QWebEngine的坑与Qt窗口切换导致的闪烁

QWebEngine的坑

1.崩溃

原因是没有delete析构。解决方法:

    delete m_webEngineView;
    m_webEngineView= nullptr;

2.闪屏

在窗口设置了下面这个属性后就会在切换窗口的时候闪屏。

setWindowFlags(Qt::FramelessWindowHint);	//去掉边框

解决方法:

    //在QApplication构造前调用,解决QWebEngine导致的闪屏问题
    QCoreApplication::setAttribute(Qt::AA_UseSoftwareOpenGL);
	QApplication a(argc, argv);

Qt窗口切换导致的闪烁

解决方法:在A窗口show之后延时关闭或隐藏B窗口。

    aWindow->show();

    //延迟关闭主窗口
    QTimer::singleShot(10, [this]() {
        hide();
    });

解决HTTPS网站无法访问的问题,解决无法录音的问题:

主要是继承QWebEnginePage,重新实现相关函数。

CustomWebEnginePage.h

#ifndef CUSTOMWEBENGINEPAGE_H
#define CUSTOMWEBENGINEPAGE_H

#include <QWebEnginePage>

class CustomWebEnginePage : public QWebEnginePage
{
	Q_OBJECT

public:
	CustomWebEnginePage(QObject *parent = Q_NULLPTR);

	// 关闭HTTPS验证
	virtual bool certificateError(const QWebEngineCertificateError &certificateError);

private Q_SLOTS:
	// 获取音频和视频捕获的权限,解决不能录音的bug
	void onFeaturePermissionRequested(const QUrl &securityOrigin, QWebEnginePage::Feature feature);

};

#endif // CUSTOMWEBENGINEPAGE_H

CustomWebEnginePage.cpp

#include "customwebenginepage.h"

CustomWebEnginePage::CustomWebEnginePage(QObject *parent/* = Q_NULLPTR*/)
	: QWebEnginePage(parent)
{
	connect(this, &CustomWebEnginePage::featurePermissionRequested, this, &CustomWebEnginePage::onFeaturePermissionRequested);
}

bool CustomWebEnginePage::certificateError(const QWebEngineCertificateError &certificateError)
{
	return true;
}

void CustomWebEnginePage::onFeaturePermissionRequested(const QUrl &securityOrigin, QWebEnginePage::Feature feature)
{
	if (feature == QWebEnginePage::MediaAudioCapture
		|| feature == QWebEnginePage::MediaVideoCapture
		|| feature == QWebEnginePage::MediaAudioVideoCapture)
		setFeaturePermission(securityOrigin, feature, QWebEnginePage::PermissionGrantedByUser);
	else
		setFeaturePermission(securityOrigin, feature, QWebEnginePage::PermissionDeniedByUser);
}

 MainWindow.cpp

    m_webView = new QWebEngineView();
	m_webView->setPage(new CustomWebEnginePage());
    m_webView->setFixedSize(QApplication::desktop()->availableGeometry().width(), QApplication::desktop()->availableGeometry().height()-40);

	QString url = CONFIG.serverUrl();
	//m_webView->load(QString("https://www.baidu.com"));
	m_webView->load(url);
    m_webView->show();

  • 8
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 14
    评论
Qt中,要实现全屏窗口的左右切换,可以使用QStackedWidget和QHBoxLayout来实现。 首先,在Qt窗口界面文件中,添加一个QStackedWidget控件,并设置其布局为水平布局(QHBoxLayout)。然后,向QStackedWidget控件中添加需要切换窗口界面,可以使用QWidget或者其他自定义的窗口类。 接下来,在控制程序的代码中,我们可以使用QPushButton或者其他按钮控件来触发切换操作。在按钮的点击事件中,我们可以通过设置QStackedWidget中当前显示的窗口的索引来切换界面。 具体的实现步骤如下: 1. 在窗口界面文件中,添加一个QStackedWidget控件,并设置为水平布局。 ```cpp QHBoxLayout *layout = new QHBoxLayout(this); QStackedWidget *stackedWidget = new QStackedWidget(this); layout->addWidget(stackedWidget); ``` 2. 向QStackedWidget控件中添加需要切换窗口界面。 ```cpp QWidget *window1 = new QWidget; QWidget *window2 = new QWidget; stackedWidget->addWidget(window1); stackedWidget->addWidget(window2); ``` 3. 在控制程序的代码中,设置按钮的点击事件,通过设置QStackedWidget中当前显示的窗口的索引来实现切换操作。 ```cpp QPushButton *leftButton = new QPushButton("左切换"); QPushButton *rightButton = new QPushButton("右切换"); connect(leftButton, &QPushButton::clicked, [=]() { int currentIndex = stackedWidget->currentIndex(); if (currentIndex == 0) { stackedWidget->setCurrentIndex(stackedWidget->count() - 1); // 切换到最后一个窗口 } else { stackedWidget->setCurrentIndex(currentIndex - 1); // 切换到前一个窗口 } }); connect(rightButton, &QPushButton::clicked, [=]() { int currentIndex = stackedWidget->currentIndex(); if (currentIndex == stackedWidget->count() - 1) { stackedWidget->setCurrentIndex(0); // 切换到第一个窗口 } else { stackedWidget->setCurrentIndex(currentIndex + 1); // 切换到下一个窗口 } }); ``` 这样,当点击左切换按钮时,会切换到前一个窗口;当点击右切换按钮时,会切换到下一个窗口。这样就实现了全屏窗口的左右切换功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 14
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值