首页 > 解决方案 > 从线程调用的类中的动画函数不起作用

问题描述

我有一个 qt5 ui 文件,我正在通过一个具有淡入/淡出功能的类加载它:

class LoadingScreen(QWidget):
    def __init__(self):
        super(LoadingScreen, self).__init__()
        loader = QUiLoader()
        file = QFile("loading_screen.ui")
        file.open(QFile.ReadOnly)
        global loading_screen
        loading_screen = loader.load(file, self)
        file.close()
        self.initUI()

    def fadeIN(self):
        self.fade_in = QPropertyAnimation(self,"windowOpacity")
        self.fade_in.setDuration(500)
        self.fade_in.setStartValue(0.0)
        self.fade_in.setEndValue(1.0)
        self.fade_in.setEasingCurve(QEasingCurve.InBack)
        self.fade_in.start()
        self.show()

    def fadeOUT(self):
        self.fade_out = QPropertyAnimation(self,"windowOpacity")
        self.fade_out.setDuration(500)
        self.fade_out.setStartValue(1.0)
        self.fade_out.setEndValue(0.0)
        self.fade_out.setEasingCurve(QEasingCurve.OutBack)
        self.fade_out.start()

我正在加载它:

global loading_screen_window
loading_screen_window = LoadingScreen()

然后我启动一个线程,它正在做一些事情,比如阅读配置:

config_thread = config(1,"config",1)
config_thread.start()

工作正常,在线程的某个点我想调用我的 LoadingScreen() 类的淡出函数。函数被执行但动画不起作用?我究竟做错了什么?在其他地方调用淡出函数表单有效,但不在我的线程内。

我正在使用 Python 2.7、Qt5 和 PySide2。

谢谢你的帮助!!

标签: pythonqt5pyside2

解决方案


我找到了一个解决方案:

class LoadingScreen(QWidget):
    def __init__(self):
        super(LoadingScreen, self).__init__()
        loader = QUiLoader()
        file = QFile("loading_screen.ui")
        file.open(QFile.ReadOnly)
        global loading_screen
        loading_screen = loader.load(file, self)
        file.close()
        self.initUI()

        #Config Thread starten
        self.config_thread = config()
        self.config_thread.finished.connect(self.fadeOUT)
        self.config_thread.start()

感谢您的帮助 eyllanesc :)


推荐阅读