[Development] QWebEnginePage and Qt 6: protected virtual functions and signals

Benjamin TERRIER b.terrier at gmail.com
Thu May 27 13:55:01 CEST 2021


On Thu, 27 May 2021 at 11:25, Allan Sandfeld Jensen <kde at carewolf.com>
wrote:

>
> I am not sure I see a significant difference. They are both compile-time.
> A
> "user" of your widget could also derive it and override the virtual
> function
> just as well as they could override your the signals?
>
>
>
No, they are not both compile-time.

With virtual functions, only subclasses that are defined at compile-time
are allowed to override.
Also, it is possible to prevent overriding using the final keyword.

With signals, the behavior can be changed at any time, including run-time,
by any part of the code.

Let's say I want a page that always ignores SSL errors.
I could do:

class CrazySSLPage final : public QWebEnginePage {
protected:
    bool certificateError(const QWebEngineCertificateError &) { return
true; }
};

Then in my code, each time I see a CrazySSLPage I am sure that it will
ignore SSL errors.

With signals I could have to do:

class CrazySSLPage final : public QWebEnginePage {
public:
    CrazySSLPage() {
        connect(this, &QWebEnginePage::certificateErrorOccured,
[this](const QWebEngineCertificateError &error) {
error.ignoreCertificateError(); });
    }
};

But then in my code, each time I see a CrazySSLPage I am never sure that it
will ignore SSL errors. Maybe someone called QObject::disconnect() on it.

CrazySSLPage page;
page.disconnect(); // It's just a QWebEnginePage now
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/development/attachments/20210527/d660e315/attachment.html>


More information about the Development mailing list