2013-03-06 109 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() 

希望幫助你。