2013-03-06 108 views
0

所有拳头我将描述我的目标:我想通知用户在阻塞模式下有一些工作正在进行中。QSplashScreen点击关闭

我希望如果我禁用点击QSplashScreen隐藏,这将符合我的需求。 在C++中,它在mousePressEvent Method的处理:

void QSplashScreen::mousePressEvent(QMouseEvent *) 

{ 
    hide(); 
} 

,所以我希望,只是重写此方法会剿隐藏,但我的代码无法正常工作:

from PyQt4 import QtGui, QtCore 
import sys 
import time 

class MySplash(QtGui.QSplashScreen): 
    def __init__(self): 
     super(MySplash, self).__init__() 
     self.setPixmap(QtGui.QPixmap("betting.gif")) 

    def mousePressEvent(self, mouse_event): 
     print('mousePressEvent', mouse_event) 

if __name__ == "__main__": 
    app = QtGui.QApplication(sys.argv) 
    splash = MySplash() 
    splash.show() 
    QtGui.qApp.processEvents() 
    print('Here I am') 
    splash.showMessage('Here I am') 
    time.sleep(2) 
    print('Do some work') 
    time.sleep(2) 
    splash.close() 

我在做什么错?

回答

0

我仍然不知道为什么重写mousePressEvent方法不起作用(我仍然有兴趣的),但我解决了我在其他的方式问题:

class BusyDialog(QtGui.QWidget): 
    def __init__(self, parent = None): 
     super(BusyDialog, self).__init__(parent) 
     self.show() 


class MainWindow(QtGui.QMainWindow): 
    def __init__(self, parent = None): 
     super(MainWindow, self).__init__(parent) 
     self.button = QtGui.QPushButton('Click me', self) 
     self.button.clicked.connect(self.button_clicked) 

    def button_clicked(self): 
     print('clicked') 
     dialog = BusyDialog() 
     QtGui.qApp.processEvents() 
     time.sleep(2) 
     print('Here I am') 
     time.sleep(2) 
     print('Do some work') 
     time.sleep(2) 
     dialog.close() 


if __name__ == "__main__": 
    app = QtGui.QApplication(sys.argv) 
    main = MainWindow() 
    main.show() 
    sys.exit(app.exec_()) 
0

回答我最初的问题很简单:mousePressEvent被宣布为受保护 - 这就是为什么无法覆盖它!

+0

如果您继承了'QSplashScreen',则不会在您的子类中阻止您重写/重新实现它。该成员函数在'QSplashScreen'中声明为虚拟保护,即它被设计为被子类覆盖。 – 2013-07-02 10:48:18

0

我发现的最简单的方法是利用python的松散函数指针。

这是我如何做到的(无需过载)。

from PyQt4.QtGui import QApplication, QPixmap, QSplashScreen 
import sys 


def MyMousePressEvent(event): 
    print "I was clicked, and I have an event!" 
    print "see its at", event 



if __name__ == "__main__": 
    app = QtGui.QApplication(sys.argv) 
    splash_image = QPixmap(":Images/MyImage.png") 
    splash = QSplashScreen(splash_image) 
    splash.mousePressEvent = MyMousePressEvent 
    splash.show() 
    ## need to check the for events on a regular interval! 
    time.sleep(2) 
    app.processEvents() 
    print('Here I am') 
    splash.showMessage('Here I am') 
    time.sleep(2) 
    QtGui.qApp.processEvents() 
    print('Do some work') 
    time.sleep(2) 
    QtGui.qApp.processEvents() 
    splash.close() 
    return app.exec_() 

,如果你想有点击与另一个对象通信,则该方法添加到该对象并设定它QSplashScreen.mousePressEvent

class MyCommClass(QMainWindow,splashscreen,parent): ## mainwindow used here but it could be whatever 

    def __init__(self,splashscreen = None,parent = None): ## Create Mainwindow Obj 
     QMainWindow.__init__(self,parent) ## Call base class CTOR 
     if(splashscreen is not None): 
      splashscreen.mousePressEvent = self.SplashMousePressEvent 
     ##instead of splash.mousePressEvent = MyMousePressEvent 

    def SplashMousePressEvent(self,event): 
     print "Splash screen was clicked!" 
     ## self.DoWhatEverIWant() 

希望帮助你。