2

I'm building a relatively simple UI using PyQt5. There are a few points in the application that run a process that takes some time, so I have used QApplication.setOverrideCursor to indicate that a process is running.

This is done through this decorator (taken from this question):

def waiting_effects(function):
    def new_function(self):
        QtWidgets.QApplication.setOverrideCursor(QtGui.QCursor(QtCore.Qt.WaitCursor))
        function(self)
        QtWidgets.QApplication.restoreOverrideCursor()
    return new_function

This works for most of the methods in the UI class except for one:

@waiting_effects
def load_data_folder(self):

        folder = QtWidgets.QFileDialog.getExistingDirectory(self)

        if folder:
            self.clear_plot(name="All")
            self.vr_headers = []
            self.ls_headers = []
            self.df = pvi.import_folder(folder)

            if self.df['voltage recording'] is not None:
                self.vr_headers = self.df['voltage recording'].columns[1:].tolist()
                self.sweeps = self.df['voltage recording'].index.levels[0]

            if self.df['linescan'] is not None:
                self.ls_headers = self.df['linescan'].columns[1::2].tolist()

                if self.df['voltage recording'] is None:
                    self.sweeps = self.df['linescan'].index.levels[0]

                self.ratio_dropdown1.clear()
                self.ratio_dropdown2.clear()
                for profile in self.ls_headers:
                    self.ratio_dropdown1.addItem(profile)
                    self.ratio_dropdown2.addItem(profile)

                self.tabWidget.setTabEnabled(2, True)

            elif self.tabWidget.isTabEnabled(2):
                self.tabWidget.setTabEnabled(2, False)

            self.update_treeWidget()

The @waiting_effects decorator in this case does not produce any change in the cursor.

Rather than using the decorator I have tried wrapping the block of code after the QFileDialog with the QApplication.setOverrideCursor, but this has not worked. I.e.:

def load_data_folder(self):

        folder = QtWidgets.QFileDialog.getExistingDirectory(self)

        QtWidgets.QApplication.setOverrideCursor(QtGui.QCursor(QtCore.Qt.WaitCursor))
        if folder:
            self.clear_plot(name="All")
            self.vr_headers = []
            self.ls_headers = []
            self.df = pvi.import_folder(folder)

            if self.df['voltage recording'] is not None:
                self.vr_headers = self.df['voltage recording'].columns[1:].tolist()
                self.sweeps = self.df['voltage recording'].index.levels[0]

            if self.df['linescan'] is not None:
                self.ls_headers = self.df['linescan'].columns[1::2].tolist()

                if self.df['voltage recording'] is None:
                    self.sweeps = self.df['linescan'].index.levels[0]

                self.ratio_dropdown1.clear()
                self.ratio_dropdown2.clear()
                for profile in self.ls_headers:
                    self.ratio_dropdown1.addItem(profile)
                    self.ratio_dropdown2.addItem(profile)

                self.tabWidget.setTabEnabled(2, True)

            elif self.tabWidget.isTabEnabled(2):
                self.tabWidget.setTabEnabled(2, False)

            self.update_treeWidget()
        QtWidgets.QApplication.restoreOverrideCursor()

The slow part of this function is the line:

self.df = pvi.import_folder(folder)

This import_folder is a function from another module I have written that loads the data folders that our acquisition software generates. I have tried wrapping just this line with QApplication.setOverrideCursor, but again that has not produced any effect.

Any thoughts / suggestions on what might be going on here?

Thanks

Community
  • 1
  • 1
dan_g
  • 2,712
  • 5
  • 25
  • 44
  • I can't test your code, but try putting `QtWidgets.qApp.processEvents()` immediately after the line that sets the cursor. – ekhumoro Dec 10 '14 at 18:54
  • Yup, that did it. Thanks! Feel free to put it in an answer and I'll mark correct – dan_g Dec 11 '14 at 21:03
  • Also, if you wouldn't mind explaining - what was the issue that that line of code fixes? – dan_g Dec 11 '14 at 21:07

1 Answers1

4

It looks like the load_data_folder function runs in the main GUI thread, so it will block any further GUI updates until it completes.

Try putting QtWidgets.qApp.processEvents() immediately after the line that sets the cursor, and that should allow the cursor to be updated before the function starts.

ekhumoro
  • 115,249
  • 20
  • 229
  • 336