我将描述我的目标:我想在阻止模式下通知用户正在进行一些工作。
我希望如果我禁用QSplashScreen中的点击隐藏,这将符合我的需要。 在C ++中,它是用mousePressEvent方法处理的:
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 :(得分: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_())
答案 1 :(得分:0)
我最初的问题的答案很简单:mousePressEvent
被声明为受保护 - 这就是无法覆盖它的原因!
答案 2 :(得分:0)
我发现的最简单的方法是利用pythons松散函数指针。
我是这样做的(不必重载)。
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()
希望对你有所帮助。