pyqt6 application is freezing while doing some long computational task

Viewed 18

I am making an desktop application with pyqt6.

On the initial screen there is a form and a pushbutton. The application takes some data from the user and does some long computational task and then redirect user to a different page. Before that page loads there is also some long task is going on on that page.

Both of these pages the application freezes while the computation is running, and unfeezes after completing the computation.

Here I am looking for two solutions.

  1. Show some loading text or screen while the computation run.
  2. Redirect user to the page while running the computation on the background.

I am posting my code to give some example.

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        if logged_in:
            self.home_page()
        else:
            self.login_page()
        self.ui.connectPushBtn.clicked.connect(partial(self.login))
        self.ui.mergeUserSavebtn.clicked.connect(self.merge_user)

    # Page Navigaitions
    def login_page(self):
        self.ui.FunctionalityStackedWidget.setCurrentWidget(self.ui.LoginStackedWidget)

    def home_page(self):

        # Some Long task is happening here
        # I want to load the UI and run the task in background here
        # Long computational code starts here <-----
        time.sleep(20000)
        # Long computational code end here <-----

        self.ui.FunctionalityStackedWidget.setCurrentWidget(self.ui.HomeScreenWidget)

    def login(self):
        ip = self.ui.ip.text()
        zk_port = self.ui.port.text() or 4370
        passwd = self.ui.password.text()
        api = self.ui.api.text()
        try:
            # Some long task is happening here after taking users input
            # I want to show some sort of loading screen here

            # Long computational code starts here <-----
            zk = ZK(
                ip=ip,
                port=zk_port,
                password=passwd,
            )
            headersList = {
                "Authorization": f"Api-Key {api}",
                "Content-Type": "application/json",
            }
            r = httpx.get(f"{BASE_URL}api_key_test/", headers=headersList)
            login_successful = zk.connect()
            # Long computational code end here <-----

            if login_successful and r.status_code == 200:
                self.home_page()  # <-- Go back to home page

        except Exception as e:
            self.ui.statuslabel.setText(f"{e}")

0 Answers
Related