QApplication.setOverrideCursor对此函数没有影响,为什么?

时间:2014-12-10 14:08:22

标签: python cursor pyqt pyqt5

我正在使用PyQt5构建一个相对简单的UI。应用程序中有几点运行需要一些时间的进程,因此我使用QApplication.setOverrideCursor来指示进程正在运行。

这是通过这个装饰器(取自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

这适用于UI类中的大多数方法,除了一个:

@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()

在这种情况下,@ waiting_effects装饰器不会在游标中产生任何变化。

我没有使用装饰器,而是尝试使用QAppile.setOverrideCursor在QFileDialog之后包装代码块,但这没有用。即:

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()

这个函数的缓慢部分是行:

self.df = pvi.import_folder(folder)

这个import_folder是我编写的另一个模块的函数,它加载了我们的采集软件生成的数据文件夹。我已经尝试用QApplication.setOverrideCursor包装这一行,但是再次没有产生任何效果。

关于这里可能会发生什么的任何想法/建议?

由于

1 个答案:

答案 0 :(得分:3)

看起来load_data_folder函数在主GUI线程中运行,因此它会阻止任何进一步的GUI更新,直到它完成。

尝试在设置光标的行之后立即放置QtWidgets.qApp.processEvents(),并且应该允许在函数开始之前更新光标。