0

I have a subclass of QWebEnginePage where I would like to override the javaSrciptConfirm function to create my own popup. It would work by starting a QEventLoop, and connecting the QPushButton.clicked.connect signals from the OK and Cancel buttons on the popup to the QEventLoop.quit slot.

I could not get this to work, as it appeared that the QEventLoop was completely bypassed immediately. So I tried with just a yellow button that shows in the top left corner, and when it is clicked it should exit the QEventLoop, and then then turn red, which still does't work:

from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtWebEngineWidgets import *
import sys

class MainWindow(QMainWindow):
  def __init__(self,parent=None,*args,**kwargs):
    QMainWindow.__init__(self,parent,*args,**kwargs)

    self._webEngine = QWebEngineView(self)
    self._webEngine.setPage(MyWebEnginePage(self._webEngine))
    self._webEngine.setUrl(QUrl("https://www.seleniumeasy.com/test/javascript-alert-box-demo.html"))

    self.setCentralWidget(self._webEngine)

    self.showMaximized()

class MyWebEnginePage(QWebEnginePage):
  def __init__(self,parent,*args,**kwargs):
    QWebEnginePage.__init__(self,parent,*args,**kwargs)

  def javaScriptConfirm(self,url,msg):
    print("in here")
    button = QPushButton(self.parent().window())
    button.setStyleSheet("background-color: yellow")
    button.show()

    loop = QEventLoop(self.parent().window())
    button.clicked.connect(loop.quit)
    loop.exec_()

    button.setStyleSheet("background-color: red")

def myExceptHook(e,v,t):
  sys.__excepthook__(e,v,t)

if __name__ == "__main__":
  sys.excepthook = myExceptHook
  app = QApplication(sys.argv)
  window = MainWindow()
  app.quit()

When the program runs, it navigates to https://www.seleniumeasy.com/test/javascript-alert-box-demo.html, and I click on the button that brings up the JS confirm box (2nd button down). In the corner of the screen, a red button shows. It should be yellow, and only turn red once I click it, but the QEventLoop never loops, it is exited from immediately. When I can get the button to stay yellow until clicked, I can then remove it and connect the popup buttons to the QEventLoop.quit slot.

I have tried making the loop variable a self.loop, different parent objects for the QEventLoop, making the button a self.button, I'm not sure why the loop quits straight away. What do I need to change in order for the QEventLoop to actually loop?

SamG101
  • 488
  • 1
  • 7
  • 18
  • Why do you use app.quit()t? In what environment are you running the application? I changed app.quit() to app.exec_() and I don't reproduce your problem on the terminal – eyllanesc Jun 20 '20 at 14:38
  • I didnt know there was a different between .exec_ and .quit; am running in the normal Python IDLE, will test the .exec_ this evening. – SamG101 Jun 20 '20 at 18:45
  • do not use IDLE, since in production you will not manage that environment, use the terminal or CMD. Also read the docs – eyllanesc Jun 20 '20 at 19:06

0 Answers0