Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Browser geolocation doesn't work with QWebEngineView



  • I am novice to QT, and I have a task to implement simple desktop wrapper on QT for our website. Everything works well except for geolocation. Every time browser has to request permission for geolocation usage nothing is happened, geolocation doesn't work. I tried a few solutions from SO, this forum and other resources, but nothing helped me. Here is my code:

    #include <QLabel>
    #include <QWebEnginePage>
    
    class WebEnginePage: public QWebEnginePage{
        Q_OBJECT
    
    public:
        WebEnginePage(QObject *parent = Q_NULLPTR):QWebEnginePage(parent) {
            connect(this, &WebEnginePage::featurePermissionRequested, this, &WebEnginePage::onFeaturePermissionRequested);
        }
    
    protected:
        bool certificateError(const QWebEngineCertificateError &certificateError){
            return true;
        }
    
    private Q_SLOTS:
        void onFeaturePermissionRequested(const QUrl &securityOrigin, QWebEnginePage::Feature feature){
            setFeaturePermission(securityOrigin, feature, QWebEnginePage::PermissionGrantedByUser);
        }
    
    };
    
    #include <QApplication>
    #include <QWebEngineView>
    #include <QWebEngineSettings>
    
    #include "main.moc"
    
    int main(int argc, char *argv[])
    {
        QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
        QApplication app(argc, argv);
    
        QWebEngineView view;
        view.setPage(new WebEnginePage());
    
        QWebEngineSettings* settings = view.settings();
    
        settings->setAttribute(QWebEngineSettings::WebAttribute::LocalStorageEnabled, true);
        settings->setAttribute(QWebEngineSettings::WebAttribute::AllowGeolocationOnInsecureOrigins, true);
        view.setUrl(QUrl(here is my url));
        view.resize(1024, 750);
        view.show();
    
        return app.exec();
    }
    

    Could you give me some suggestions, what's wrong with my code?


Log in to reply