I'm using C++ with Qt v5.9.1.
I'm dealing with a problem when I use QWebEngineView. It freezes in the AutoScrolling, then it unfreezes and returns to normal after the AutoScrolling finish.
Freezes:
void MainWindow::on_BTN_Load_clicked()
{
QUrl url = QUrl(ui->lineEdit->text());
webview->page()->load(url);
connect(webview->page(), &QWebEnginePage::loadFinished, this, &MainWindow::on_PageLoadFinished);
}
void MainWindow::on_PageLoadFinished()
{
disconnect(webview->page(), &QWebEnginePage::loadFinished, this, &MainWindow::on_PageLoadFinished);
//Get the page's height
webview->page()->runJavaScript("document.body.scrollHeight",[&](const QVariant &result) {set_PageHeight(result.toInt());});
}
void MainWindow::set_PageHeight(int TheHeight)
{
ThePageHeight = TheHeight;
ScrollDownPage();
}
void MainWindow::ScrollDownPage()
{
TheCurrentVerticalScroll = 0;
while (TheCurrentVerticalScroll <= ThePageHeight)
{
webview->page()->runJavaScript(QString("window.scrollTo(0, %1);").arg(TheCurrentVerticalScroll));
TheCurrentVerticalScroll += 100;
QEventLoop loop;
QTimer t;
t.connect(&t, &QTimer::timeout, &loop, &QEventLoop::quit);
t.start(30);
loop.exec();
qApp->processEvents();
}
}
Works:
But, this problem doesn't appear when I click manually on a button that call the same AutoScrolling function.
void MainWindow::on_BTN_Load_clicked()
{
QUrl url = QUrl(ui->lineEdit->text());
webview->page()->load(url);
connect(webview->page(), &QWebEnginePage::loadFinished, this, &MainWindow::on_PageLoadFinished);
}
void MainWindow::on_PageLoadFinished()
{
disconnect(webview->page(), &QWebEnginePage::loadFinished, this, &MainWindow::on_PageLoadFinished);
//Get the page's height
webview->page()->runJavaScript("document.body.scrollHeight",[&](const QVariant &result) {set_PageHeight(result.toInt());});
}
void MainWindow::set_PageHeight(int TheHeight)
{
ThePageHeight = TheHeight;
// ScrollDownPage(); //I disable this now
}
void MainWindow::ScrollDownPage()
{
TheCurrentVerticalScroll = 0;
while (TheCurrentVerticalScroll <= ThePageHeight)
{
webview->page()->runJavaScript(QString("window.scrollTo(0, %1);").arg(TheCurrentVerticalScroll));
TheCurrentVerticalScroll += 100;
QEventLoop loop;
QTimer t;
t.connect(&t, &QTimer::timeout, &loop, &QEventLoop::quit);
t.start(30);
loop.exec();
qApp->processEvents();
}
}
//I add this:
void MainWindow::on_BTN_Scroll_clicked()
{
ScrollDownPage();
}
What I want:
I want to scroll the web page automatically, not manually by clicking the button.
Thanks.