QtCore.SIGNALS 不适用于我的代码 [英] QtCore.SIGNALS not working on my code

查看:72
本文介绍了QtCore.SIGNALS 不适用于我的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以解释我应该如何喜欢我的代码,或者我做错了什么?我想使用按钮btn_run"来运行view_splash"功能.但是有些想法出错了,但是 'view_splash' 不会启动.它显示我没有错误.

Can someone me explane how should like my code, or what I'm doing wrong ? I want to use button 'btn_run' to run 'view_splash' function. But somethink going wrong, but 'view_splash' won't start. It show me no errors.

import sys
from PyQt4 import QtGui, QtCore
import time


class Window(QtGui.QMainWindow):

 def __init__(self):
    super(Window, self).__init__()
    self.setGeometry(500, 150, 500, 600)
    self.setWindowTitle('Test GUI')

    self.threadclass = AThread()
    self.connect(self.threadclass, QtCore.SIGNAL("view_splash()"), self.view_splash)

    self.home()

 def home(self):
    btn_run = QtGui.QPushButton("Run", self)
    self.threadclass = AThread()
    btn_run.clicked.connect(self.threadclass.start)
    btn_run.resize(120, 40)
    btn_run.move(190, 540)

    self.show()

 def view_splash(self):
    print('test')
    label = QLabel("<font color=red size=10<b>" + "SPLASH" + "</b></font>")
    label.setWindowFlags(Qt.SplashScreen | Qt.WindowStaysOnTopHint)
    label.show()
    QtCore.QTimer.singleShot(5000, label.close)


class AThread(QtCore.QThread):
 def __init__(self):
    super(AThread, self).__init__()

 def run(self):
    print(1)
    print(2)
    time.sleep(5)
    print(3)
    print(4)
    self.emit(QtCore.SIGNAL("view_splash()"))


 app = QtGui.QApplication(sys.argv)
 GUI = Window()
 sys.exit(app.exec_())

推荐答案

您需要以不同的方式创建和连接信号.

You need to create and connect the signals differently.

class AThread(QtCore.QThread):
    view_splash = QtCore.pyqtSignal()

    def run(self):
        ...
        self.view_splash.emit()

class Window(QtGui.QMainWindow):

    def __init__(self):
        ...
        self.threadclass.view_splash.connect(self.view_splash)

这篇关于QtCore.SIGNALS 不适用于我的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆