PyQt:如何使用自定义事件创建自定义QAction

2024-03-29 06:49:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试将鼠标事件添加到一个新的QAction对象中。我想在自定义菜单中使用它们

from PyQt4 import QtCore, QtGui
from PyQt4.QtCore import QEvent, pyqtSignal as Signal, pyqtSlot as Slot

class MouseEvent(QEvent):
    def __init__(self):
        super(MouseEvent,self).__init__(QEvent.Type(QEvent.MouseButtonRelease))

class MyAction(QtGui.QAction):
    clicked = Signal()

    def __init__(self, name, parent):
        super(MyAction, self).__init__(name, parent)
        self.customEvent(MouseEvent)

    def mousePressEvent(self, event):
        if event.button() == QtCore.Qt.RightButton:
            event.accept()
            self.clicked.emit()
            self.rightClicked(event)
        else:
            event.ignore()

    @Slot()
    def rightClicked(self, event):
        print "right clicked"
        return event

class AnyApplication(QtGui.QMainWindow):
    def __init__(self):
        super(AnyApplication, self).__init__()
        self.UI()

    def UI(self):
        menuBar = self.menuBar()
        m = menuBar.addMenu("Edit")
        a = MyAction("Do", m)
        #a.clicked.connect(self.doMoreWithClicked)
        m.addAction(a)

    def doMorWithClicked(self):
        print "Do more.."

app = QtGui.QApplication(sys.argv)
anyApp = AnyApplication()
anyApp.show()
sys.exit(app.exec_())

我得到以下错误:

^{pr2}$

我真的很想了解我是如何创建自己的事件,并将它们与信号和时隙相结合。在

谢谢。。。在


Tags: selfeventinitdef事件classsuperqtgui