Unsolved QApplication creates new thread any time
-
Hey, I'm trying to render a page few times, but unfortunately
QApplication
creates a new thread every time and when i'm trying to run the rendering on the second time it's saying:
QApplication was not created in the main() thread.
In addition i am getting the following error:QApplication::exec must be called on main thread
I tried to create Qapplication object only once, but the trigger for the render method is an event recieved from socketIO module.
At the first time the render does works. At the second time the render crash the whole applicationHere is a snipped code:
def __init__(self, table_name=None, db_type=None, *args, **kwargs): self.app = QApplication(sys.argv) self.app.setApplicationName(QString("Chrome")) self.app.setApplicationVersion(QString("53.0.2785.113")) super(MainWindow, self).__init__(*args, **kwargs) self.setWindowTitle("ScarpSite") self.browser = QWebView() self.networkAccessManager = QNetworkAccessManager() self.cookieJar = QNetworkCookieJar() self.__VulnCrud = VulnerabilitiesCRUD self.__tableName = table_name self.get_configuration_properties() def ScanPage(self, pageEntity=None, forms=None, links=None, vulnUtils=None): self.forms = forms self.links = links self.vulnUtils = vulnUtils self.updateCookiesMechanizetoQt(self.vulnUtils.getCookieJar()) self.url = pageEntity.getURL() self.domain = urlparse(self.url).hostname self.browser.loadFinished.connect(self.__onUrlLoaded) self.browser.page().setNetworkAccessManager(self.networkAccessManager) self.browser.page().userAgentForUrl(QUrl(self.url)) curURL = QUrl(self.url) self.browser.load(curURL) self.setCentralWidget(self.browser) self.show() self.app.exec_()
Any ideas how can i solve it?
-
- You show an
__init__()
method, but not what class it is init'ing. I don't know whatself
is. - I don't see anything about threads in your code.
- You show a
ScanPage()
method, but not where it gets called. - The
__init__()
creates theQApplication
. As I said I don't know what it is init'ing, but that's probably not the place to create the application. - I don't know what
ScanPage
is/where it's called from, but it's probably not the place to callQApplication.exec_()
from.
Concentrate on the error messages. You should not be calling
QApplication
more than once. You must create it from themain()
thread (the bit where you test forif__name__== "__main__"
). Same for the call toexec_()
. Whatever you mean about "but the trigger for the render method is an event recieved from socketIO module" needs sorting out to obey this (e.g. some sort of you only create theQApplication
once at the start, outside of the this, in the__main__
). - You show an