How to refresh a QApplication using QWebEngineView every with an schedule period

2.4k Views Asked by At

I'm trying to reload an URL in a given period using PYQT5, QWebEngineView, and schedule as far as I've search the code stops its execution after the app_exec(). So the "while" cicle and "scheduler" never gets executed.

as far as i have search there are diferent ways to reload the page (reload, refresh, repaint) but I'cant find the way to get this executed during the webpage execution.

any idea?

import sys
from PyQt5.QtWidgets import QApplication
from PyQt5.QtCore import QUrl
from PyQt5.QtWebEngineWidgets import QWebEngineView
import schedule, time


def job():
    print('test')
    print('Reload :%s',time.time())
    browser.reload()
    browser.refresh()
    browser.sender().reload()
    browser.repaint()    


url ='http://www.google.com'

app = QApplication(sys.argv)
browser = QWebEngineView()
browser.load(QUrl(url))
browser.showFullScreen()

schedule.every(0.5).minutes.do(job)

sys.exit(app.exec_()) 

while True:
    schedule.run_pending()
    time.sleep(1)
1

There are 1 best solutions below

0
On

app.exec_() is the event loop that allows the GUI to execute its tasks such as listening to events and updating the GUI states, the event loop is similar to a while True: doSomething(), so the code that is after it will only be executed if the GUI is finished running. On the other hand if you want to execute periodic tasks you must use QTimer:

import sys
import time
from PyQt5 import QtCore, QtWidgets, QtWebEngineWidgets

def job():
    print('test')
    print('Reload :%s',time.time())
    browser.reload() 

if __name__ == '__main__':
    url ='http://www.google.com'

    app =  QtWidgets.QApplication(sys.argv)
    browser = QtWebEngineWidgets.QWebEngineView()
    browser.load(QtCore.QUrl(url))
    browser.show()
    timer = QtCore.QTimer(interval=30*1000)
    timer.timeout.connect(job)
    timer.start()
    sys.exit(app.exec_())