Custom error pages (404, 403 etc) and setHtml
-
I'm in PyQt5. How do I set my own error pages for QWebEngine, like when the page is not found or it's forbidden? I'd like a different error page for every error.
I read this but it didn't help at all:
https://stackoverflow.com/questions/51656117/how-to-get-http-status-codes-from-qt-qwebengineview/53516333#53516333Also, I'm thinking of replacing the error pages with custom html that's hardcoded into the source code like this:
self.tabs.currentWidget().setHtml("Bad syntax<br>You have to start your address with 'http'.")
But I have a problem with this setHtml method. It automatically puts this in the url bar (lineEdit):
"data:text/html;charset=UTF-8,Bad syntax%3Cbr%3EYou have to start your address with %27http%27."
How do I stop this from happening and instead retain what user wrote on the url bar? Of course I tried urlbar.setText, but it's somehow ignored in this case, unless load the page twice. -
I worked around the urlbar text thing with this.
if url.startswith("data:"): text = self.urlbar.text() self.urlbar.setText(text)
It basically works, because setHtml always forces something starting with "data:" into the urlbar.
As far as the custom Html status codes / error messages, I can make a custom 404.html page on my Apache server and direct my .htaccess file to it, but that's how far I can go.
Is there any "onLoadFailed" type of syntax in PyQt where I could check if a page was able to be loaded at all? I've disabled Chromium's status codes with this:settings = QWebEngineSettings.globalSettings() settings.setAttribute(QWebEngineSettings.ErrorPageEnabled, False)
but if a page is offline for example, all I get is a blank page which is confusing to users.