動的にメソッドを追加する

2016/08/15

Python2.7.6, PySide1.2.4

import sys
from PySide import QtCore,QtGui
from types import MethodType

import ui

class MainWindow(QtGui.QWidget):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.ui = ui.Ui_Form()
        self.ui.setupUi(self)
        self.ui.graphicsView.mouseMoveEvent = MethodType(new_event,self,MainWindow)


def new_event(self, event):
    print('mouseMoveEvent: pos {}'.format(event.pos()))

if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())