Qt4 QSplashScreen单击即关闭
首先,我将描述我的目标:我想在阻塞模式下通知用户,有一些工作正在进行中 我希望如果我在QSplashScreen中禁用单击隐藏,这将符合我的需要。 在C++中,它使用MouthPrimeSevin方法:Qt4 QSplashScreen单击即关闭,qt4,pyqt4,Qt4,Pyqt4,首先,我将描述我的目标:我想在阻塞模式下通知用户,有一些工作正在进行中 我希望如果我在QSplashScreen中禁用单击隐藏,这将符合我的需要。 在C++中,它使用MouthPrimeSevin方法: void QSplashScreen::mousePressEvent(QMouseEvent *) { hide(); } 所以我希望重写此方法将抑制隐藏,但我的代码不起作用: from PyQt4 import QtGui, QtCore import sys import ti
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()
我做错了什么?我仍然不知道为什么重写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_())
我最初问题的答案很简单:
mousePressEvent
被声明为受保护-这就是为什么不可能覆盖它 我发现的最简单的方法是利用pythons松散的函数指针
以下是我如何做到这一点(不必超负荷)
如果希望单击与其他对象通信,请将该方法添加到该对象,并将其设置为qsplashsscreen.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()
希望这对您有所帮助。如果您继承了
qsplashsscreen
则没有任何东西可以阻止您在子类中重写/重新实现它。成员函数在qsplashsscreen
中声明为虚拟受保护,即它被设计为被子类覆盖。
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()