2

首先,我将描述我的目标:我想在阻塞模式下通知用户有一些工作正在进行中。

我希望如果我在 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()

我做错了什么?

4

3 回答 3

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()

希望对您有所帮助。

于 2013-06-27T15:51:58.103 回答
0

我最初的问题的答案很简单:mousePressEvent被声明为受保护的——这就是为什么不能覆盖它的原因!

于 2013-03-07T09:13:19.313 回答
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_())
于 2013-03-06T14:39:06.927 回答