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

Disable sound in QWebPage



  • Is it possible to disable sound in QWebPage ? Till now I've managed to completely disable javascript pop-ups etc using this code:

    @class CustomWebPage : public QWebPage {
    protected:
    virtual QString chooseFile(QWebFrame , const QString&) { return QString(); }
    virtual void javaScriptAlert (QWebFrame
    , const QString &) {}
    virtual bool javaScriptConfirm(QWebFrame *, const QString &) { return false; }
    virtual bool javaScriptPrompt(QWebFrame *, const QString &, const QString &, QString *) { return false; }
    };@

    I also tried muting any <audio> and <video>, but unsuccessfully:

    @QWebElement bodyElem = webPage->mainFrame()->documentElement().findFirst("body");

    bodyElem.evaluateJavaScript("var mute=function(tag){"\
                                             "    var elems = document.getElementsByTagName(tag);"\
                                             "    for(var i = 0; i < elems.length; i++){"\
                                             "        elems[i].muted=true;" \
                                             "    }    "\
                                             "}"\
                                             "mute(\"video\");"\
                                             "mute(\"audio\");"\
                                             );@
    

    While the above code works here, for example: http://www.w3schools.com/tags/tryit.asp?filename=tryhtml5_audio_muted

    But this was just a workaround. I would like, if possible, to mute the whole QWebPage.



  • Thanks, your js code works, but only after some time (when page already loaded)


Log in to reply