0

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}")

Nahidujjaman Hridoy
  • 1,175
  • 12
  • 28
  • Related: [Run Function in the Background and Update UI](https://stackoverflow.com/q/47560399/8746648) – asynts Sep 08 '22 at 06:49
  • Your code contains absolutely *nothing* about that "computational task", there's only some comment put as a placeholder. You also used the `multithreading` tag, but there's no trace of it in either the question body or its code. Sorry, but this is not a tutorial service, if you tried to do something, please [edit] your post and provide a [mre] based on those attempts. – musicamante Sep 08 '22 at 06:54
  • @musicamante I disagree, I feel like the question is fine. It's not *good* but it's not bad either. He clearly explains the problem and while his code can't be executed directly it mostly resembles a MCVE. It's a duplicate for sure, but I feel your comment is a bit too harsh for that. – asynts Sep 08 '22 at 07:00
  • @musicamante is the code okay now? I have updated it. – Nahidujjaman Hridoy Sep 08 '22 at 07:05
  • 1
    @asynts I may have been harsh, but the very fact that the OP used that tag is quite suspicious: they seem to *know* that they need multithreading, but there's no visible attempt in doing that. And since this kind of "broad" issue is practically seen on a daily basis on PyQt/PySide tags (but, for obvious reasons, could potentially have different practical solutions that aren't clear to unexperienced users), seeing that specific tag is a sufficient alert from me, as too similar to common questions saying "I know I should use treading, but I don't know how". – musicamante Sep 08 '22 at 07:20

0 Answers0