使用Qt WebEngine自动授予对麦克风和摄像头的访问权限 [英] Auto Grant access to Mic and Camera with Qt WebEngine

查看:1580
本文介绍了使用Qt WebEngine自动授予对麦克风和摄像头的访问权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用RPi和WebRTC构建家庭安全系统.我只需要一种触发浏览器打开给定URL并自动授予对麦克风和摄像头访问权限的方法.我曾希望将WebEngine库与PyQt一起使用,但是Ry的PyQt不支持WebEngine.因此,我现在正在尝试Qt本身.不幸的是,我不熟悉C ++,所以我很挣扎.

I am building a home security system with RPi and WebRTC. I simply need a way to trigger a browser to open at a given URL and to auto-grant access to the Microphone and Camera. I had hoped to use the WebEngine library with PyQt but WebEngine is not supported in PyQt for RPi. So I am trying Qt itself now. Unfortunately I am not familiar with C++, so i am struggling.

示例此处有90我需要的百分比.该代码复制如下.我只需要调整它即可在需要时授予对麦克风和摄像头的访问权限.我希望有人可以帮助我吗?

The example here has 90% of what I need. The code is replicated below. I just need to tweak it to grant access to the mic and camera when it is requested. I am hoping someone can assist me with this?

#include <QApplication>
#include <QWebEngineView>

QUrl commandLineUrlArgument()
{
    const QStringList args = QCoreApplication::arguments();
    for (const QString &arg : args.mid(1)) {
        if (!arg.startsWith(QLatin1Char('-')))
            return QUrl::fromUserInput(arg);
    }
    return QUrl(QStringLiteral("https://www.qt.io"));
}


int main(int argc, char *argv[])
{
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    QApplication app(argc, argv);
    QWebEngineView view;
    view.setUrl(commandLineUrlArgument());
    view.resize(1024, 750);
    view.show();

    return app.exec();
}

推荐答案

我回答了这个问题,但对于PyQt5:

I answered this question but for PyQt5: Grant access to Cam & Mic using Python for PyQt WebEngine, I will only do a C ++ translation to Python, the base is the same.

#include <QApplication>
#include <QUrl>
#include <QWebEngineView>

class WebEnginePage: public QWebEnginePage{
    Q_OBJECT
public:
    WebEnginePage(QObject *parent = Q_NULLPTR):QWebEnginePage(parent){
        connect(this, &WebEnginePage::featurePermissionRequested, this, &WebEnginePage::onFeaturePermissionRequested);
    }
private Q_SLOTS:
    void 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);
    }
};

QUrl commandLineUrlArgument()
{
    const QStringList args = QCoreApplication::arguments();
    for (const QString &arg : args.mid(1)) {
        if (!arg.startsWith(QLatin1Char('-')))
            return QUrl::fromUserInput(arg);
    }
    return QUrl(QStringLiteral("https://www.qt.io"));
}


int main(int argc, char *argv[])
{
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    QApplication app(argc, argv);
    QWebEngineView view;
    view.setPage(new WebEnginePage);
    view.setUrl(commandLineUrlArgument());
    view.resize(1024, 750);
    view.show();

    return app.exec();
}

#include "main.moc"

这篇关于使用Qt WebEngine自动授予对麦克风和摄像头的访问权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆