Dalamber Dalamber - 2 months ago 36
C++ Question

One QWebEnginePage for two or more QWebEngineView

I have one instance of QWebEnginePage that is loaded with some url (e.g. https://www.google.com)

I want this page to be displayed in two QWebEngineViews, but it looks like Qt doesn't allow that.

Once I call setPage on one view, the other view stops rendering the page.
Looks like it is a matter of ownership or something like that, but I don't see any information about that in Qt docs.

Anyone else faced such issue?

auto p = new QWebEnginePage();
p->load(QUrl("https://www.google.com"));

auto w1 = new QWebEngineView();
auto w2 = new QWebEngineView();

w1->setPage(p);
w2->setPage(p);

w1->show();
w2->show();


I expect both views to display the same page, but only w2 shows it. If you later call
w1->setPage(p)
- the page will be displayed on w1 and will disappear from w2.

Joe Joe
Answer

Like most classes in Qt, QWebEnginePage is a QObject. QObject always have exactly one parent (owner). The same is true for QWebEnginePage. Furthermore, it has a method QWebEnginePage::view returns a pointer to the QWidget currently showing the page. Therefore, it can by definition only have a single view.

I would suggest creating two QWebEnginePage instances and connecting the signals to their respective slots (or lambdas which call a corresponding non-slot setter) of the other page. Some like scrollPositionChanged might not have corresponding setters, so you might be out of luck for perfect synchronization.