0

我正在使用 QT5 制作一个简单的浏览器。

我有QMainWindow一个QWebEngineView里面有一个,我试图让它自动接受权限请求,但我似乎无法让它工作......(稍后我会提示用户)

我在网上查看并找到了一些东西,但该解决方案对我不起作用,因为他们设置的应用程序与我不同。

main.cpp只是带有声明MainWindow并显示它的默认值

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QUrl>
#include <QWebEngineView>
#include <QWebEnginePage>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    connect(ui->view->page(), ui->view->page()->featurePermissionRequested, this, this->onFeaturePermissionRequested);
}


MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_lineEdit_returnPressed()
{
    QString url = ui->lineEdit->text();
    QUrl curl;
    curl.setUrl(url);
    ui->view->setUrl(curl);
}

void MainWindow::on_back_pressed()
{
    ui->view->back();
}


void MainWindow::on_forward_pressed()
{
    ui->view->forward();
}


void MainWindow::on_reload_pressed()
{
    ui->view->reload();
}


void MainWindow::on_view_urlChanged(const QUrl &arg1)
{
    QString newurl = arg1.toString();
    QStringList urllist = newurl.split('?');
    ui->lineEdit->setCursorPosition(0);
    ui->lineEdit->setText(urllist[0]);
}

void MainWindow::onFeaturePermissionRequested(const QUrl &securityOrigin, QWebEnginePage::Feature feature)
{
    ui->view->page()->setFeaturePermission(securityOrigin, feature, QWebEnginePage::PermissionPolicy(1));
}

头文件是这样的:

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QWebEnginePage>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = nullptr);
    ~MainWindow();


private slots:
    void on_lineEdit_returnPressed();

    void on_back_pressed();

    void on_forward_pressed();

    void on_reload_pressed();

    void on_view_urlChanged(const QUrl &arg1);

    void onFeaturePermissionRequested(const QUrl &securityOrigin, QWebEnginePage::Feature feature);

private:
    Ui::MainWindow *ui;
};


#endif // MAINWINDOW_H

每当我尝试构建它时,我都会收到这些消息

/home/kiwifruit555/Documents/kUwU/Web/mainwindow.cpp:12: error: invalid use of non-static member function ‘void QWebEnginePage::featurePermissionRequested(const QUrl&, QWebEnginePage::Feature)’
../Web/mainwindow.cpp: In constructor ‘MainWindow::MainWindow(QWidget*)’:
../Web/mainwindow.cpp:12:49: error: invalid use of non-static member function ‘void QWebEnginePage::featurePermissionRequested(const QUrl&, QWebEnginePage::Feature)’
   12 |     connect(ui->view->page(), ui->view->page()->featurePermissionRequested, this, this->onFeaturePermissionRequested);
      |                               ~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/qt/QtWebEngineWidgets/QWebEnginePage:1: In file included from /usr/include/qt/QtWebEngineWidgets/QWebEnginePage:1,
In file included from /usr/include/qt/QtWebEngineWidgets/QWebEnginePage:1,
                 from ../Web/mainwindow.h:5,
                 from ../Web/mainwindow.cpp:1:
/usr/include/qt/QtWebEngineWidgets/qwebenginepage.h:342:10: note: declared here
  342 |     void featurePermissionRequested(const QUrl &securityOrigin, QWebEnginePage::Feature feature);
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~

我尝试了多种方法,但似乎无法使其正常工作...

4

1 回答 1

2

连接语法应该是:

connect(ui->view->page(), &QWebEnginePage::featurePermissionRequested, this, &MainWindow::onFeaturePermissionRequested);

更多信息请阅读新的信号槽语法

明确地使用枚举值而不是数值也更好:

ui->view->page()->setFeaturePermission(securityOrigin, feature, QWebEnginePage::PermissionGrantedByUser);
于 2021-07-12T02:11:19.847 回答