我尝试将鼠标事件添加到新的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_())
我收到以下错误:
Traceback (most recent call last):
...
self.event(MouseEvent)
TypeError: QAction.event(QEvent): argument 1 has unexpected type 'PyQt4.QtCore.pyqtWrapperType'
我真的很想了解如何创建自己的事件并将它们与信号和插槽结合起来。
...谢谢