Methods to wait for the webpage to load finish?
-
void MainWindow::on_pushButton_clicked() { QString modules=stringpath; QUrl url2(modules); innerPage->setUrl(url2);//innerpage is a QWebview connect(innerPage, SIGNAL(loadFinished(bool)), SLOT(parse(bool))); } void MainWindow::parse(bool) { QWebFrame *frameInner = innerPage->page()->mainFrame(); QWebElement doc = frameInner->documentElement(); QString json=doc.toPlainText(); //parsing code print("parsing executed"); }
i realised i put on_pushButton_clicked in a loop as such:
int i=0; While(i!=2){ i++; print(i) on_pushbutton_clicked(); }
outcome:
1
2
parsing executed.i want parsing so be executed in every loop however i cant seem to be able to solve it?
-
Move the connect out of
on_pushButton_clicked
or you'll raise multiple slots. put it where you constructinnerPage
(you are also missingthis
as receiver in the connect)change your loop to:
QEventLoop waitParse; connect(innerPage, SIGNAL(loadFinished(bool)), &waitParse, SLOT(quit())); for(int i=0;i<2;++i){ on_pushbutton_clicked(); waitParse.exec(); }
-
void MainWindow::on_pushButton_clicked() { QString modules=stringpath; QUrl url2(modules); innerPage->setUrl(url2);//innerpage is a QWebview connect(innerPage, SIGNAL(loadFinished(bool)), SLOT(parse(bool))); } void MainWindow::parse(bool) { QWebFrame *frameInner = innerPage->page()->mainFrame(); QWebElement doc = frameInner->documentElement(); QString json=doc.toPlainText(); //parsing code print("parsing executed"); }
i realised i put on_pushButton_clicked in a loop as such:
int i=0; While(i!=2){ i++; print(i) on_pushbutton_clicked(); }
outcome:
1
2
parsing executed.i want parsing so be executed in every loop however i cant seem to be able to solve it?
-
Move the connect out of
on_pushButton_clicked
or you'll raise multiple slots. put it where you constructinnerPage
(you are also missingthis
as receiver in the connect)change your loop to:
QEventLoop waitParse; connect(innerPage, SIGNAL(loadFinished(bool)), &waitParse, SLOT(quit())); for(int i=0;i<2;++i){ on_pushbutton_clicked(); waitParse.exec(); }
@VRonin In my opinion it would be better to not to use QEventLoop. He can simply take next URL in void MainWindow::parse(bool) if there is any.
Actually @J-Hilk already suggested it in the other thread- Put all URLs in a container
- Start with the first one
- In MainWindow::parse(bool) take the next one from the container
Original thread: https://forum.qt.io/topic/92220/does-a-loop-continue-running-even-if-signals-and-slot-is-not-complete