I am making a simple browser using QT5.
I have a QMainWindow
with a QWebEngineView
inside of it and I am trying to make it so that it auto accepts permission requests but I can't seem to get it to work... (Later I will make it prompt the user)
I looked online and found something but the solution didn't work for me as they set their application up differently than I did.
The main.cpp
is simply the default with a declaration of MainWindow
and showing it
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));
}
And the header file is as so:
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
Whenever I attempt to build it I get these messages
/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);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~
I have tried multiple things but I cannot seem to get it to work...
The connection syntax should be:
connect(ui->view->page(), &QWebEnginePage::featurePermissionRequested, this, &MainWindow::onFeaturePermissionRequested);
Fore more information read New Signal Slot Syntax.
It is also better to use the enum value explicitly instead of the numeric value:
ui->view->page()->setFeaturePermission(securityOrigin, feature, QWebEnginePage::PermissionGrantedByUser);