0

How can I run the PyQt window method MainWindow.load_url() from the main process?

from multiprocessing import Process
import sys
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QMainWindow
from PyQt5.QtCore import *
from PyQt5.QtWebKitWidgets import *


class MainWindow(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)

        self.desktop_size = QtWidgets.QDesktopWidget().screenGeometry()
        self.__browser = QWebView(self)
        # todo
        self.init_ui()

    def init_ui(self):
        self.__browser.resize(self.desktop_size.width(), self.desktop_size.height())
        self.load_url('https://stackoverflow.com/')

        self.showFullScreen()

    def load_url(self, url: str):
        self.__browser.load(QUrl(url))


def run_qt():
    app = QtWidgets.QApplication(sys.argv)
    x = MainWindow()
    sys.exit(app.exec_())


class QtGui:
    process = None

    @staticmethod
    def read_process():
        if QtGui.process is None:
            QtGui.process = Process(target=run_qt)
        return QtGui.process

    @staticmethod
    def stop_process():
        if QtGui.process is not None:
            if QtGui.process.is_alive():
                QtGui.process.terminate()
                QtGui.process = None

    @staticmethod
    def start_process():
        if QtGui.read_process().is_alive() is False:
            QtGui.read_process().start()


if __name__ == '__main__':
    qtGui = QtGui()
    qtGui.start_process()

    """
        how to run method MainWindow.load_url() from here
    """

I tried to create an instance of QWebKit in the main process and pass it as an argument to have access to it in two processes, but PyQt does not accept it, which I understand. Unfortunately I don't know how I can do it. Thanks

Kap
  • 35
  • 4
  • 1
    First of all, *never* use existing class/submodule names: QtGui is part of PyQt, so, using that is simply *wrong*. Besides that, what is the reason for using Process to run a QApplication? – musicamante Mar 07 '20 at 14:02
  • This is one part of the application. From the main process, I manage several other processes. Other processes are not associated with PyQt. – Kap Mar 07 '20 at 14:07
  • Qt has its own event loop, which might interfere with others. Besides that, communication with QObjects from other threads always have to happen through signals and slots. Finally, provide a valid [minimal, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) of a typical situation, since your code just start the process, but obviously quits the program afterwards. – musicamante Mar 07 '20 at 16:25

0 Answers0