【问题标题】:QApplication.setOverrideCursor has no effect on this function, why?QApplication.setOverrideCursor 对这个函数没有影响,为什么?
【发布时间】:2015-02-08 18:30:38
【问题描述】:

我正在使用 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 装饰器不会对光标产生任何变化。

我尝试使用 QApplication.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 包装这一行,但同样没有产生任何效果。

对这里可能发生的事情有什么想法/建议吗?

谢谢

【问题讨论】:

  • 我无法测试您的代码,但请尝试将QtWidgets.qApp.processEvents() 紧跟在设置光标的行之后。
  • 是的,做到了。谢谢!随意把它放在答案中,我会标记正确
  • 另外,如果您不介意解释一下 - 那行代码修复了什么问题?

标签: python cursor pyqt pyqt5


【解决方案1】:

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

尝试将QtWidgets.qApp.processEvents() 紧跟在设置光标的行之后,这样应该可以在函数启动之前更新光标。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-01-18
    • 2019-09-03
    • 1970-01-01
    • 2018-06-04
    • 2013-05-19
    • 1970-01-01
    • 2019-09-28
    • 1970-01-01
    相关资源
    最近更新 更多