[PyQt] QWebEnginePage.LifecycleState doesn't work as expected in PyQt while the official C++ lifecyle example works

Coiby Xu coiby.xu at gmail.com
Sun Apr 19 05:03:30 BST 2020


>The only way I can determine if there is a PyQt-specific problem is to
>compare it with a C++ equivalent of the above code that behaves
>differently.


Here is the C++ equivalent code which shows it's not a PyQt-specific
issue,

1. minimal.pro

TEMPLATE = app
QT += webenginewidgets
SOURCES += main.cpp

2. main.cpp

#include <QApplication>
#include <QWebEngineView>
#include <QTabWidget>
#include <QWebEnginePage>
#include <QTimer>

QTabWidget *tab_widget_;

QWebEngineView *views_[2];

void current_tab(int i) {
     printf("%d state=%d\n", i, (int)views_[i]->page()->lifecycleState());
}

void discard_tab() {
     QWebEnginePage *page;
     for (int i=0; i<2; i++) {
         page = views_[i]->page();
         page->setLifecycleState(QWebEnginePage::LifecycleState::Discarded);
         printf("%d state=%d\n", i, (int)page->lifecycleState());
     }
}

int main(int argc, char *argv[])
{
     QApplication app(argc, argv);

     QUrl qurl = QUrl(QStringLiteral("http://example.com"));

     QTabWidget tab_widget;
     tab_widget_ = &tab_widget;
     QWebEngineView views[2];
     for (int i=0; i<2; i++) {
         views_[i] = &views[i];
         views[i].setUrl(qurl);
         views[i].resize(1024, 750);
         tab_widget.addTab(&views[i], "tab1");
     }

     // discard tab after
     QTimer::singleShot(2000, &discard_tab);
     printf("discard=%d\n", (int)QWebEnginePage::LifecycleState::Discarded);
     QObject::connect(&tab_widget, &QTabWidget::currentChanged, &current_tab);

     tab_widget.show();

     return app.exec();
}

FWIW I've reported it to https://bugreports.qt.io/browse/QTWB-49.

Thank you for the reply!



More information about the PyQt mailing list