Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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 application

    Here 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?



  • @zur1

    • You show an __init__() method, but not what class it is init'ing. I don't know what self 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 the QApplication. 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 call QApplication.exec_() from.

    Concentrate on the error messages. You should not be calling QApplication more than once. You must create it from the main() thread (the bit where you test for if__name__== "__main__"). Same for the call to exec_(). 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 the QApplication once at the start, outside of the this, in the __main__).


Log in to reply