Created
April 4, 2013 14:00
-
-
Save durden/5310601 to your computer and use it in GitHub Desktop.
Example of where using pyqtSignal.connect as a decorator will NOT work
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from PyQt4.QtCore import pyqtSignal, QObject | |
from PyQt4.QtGui import QApplication, QFrame, QGridLayout, QPushButton | |
class View(QFrame): | |
openStuff = pyqtSignal() | |
def __init__(self, parent=None): | |
super(View, self).__init__(parent) | |
self._button = QPushButton('Open') | |
# Notify outside world (controller) | |
self._button.clicked.connect(self.openStuff.emit) | |
layout = QGridLayout() | |
layout.addWidget(self._button) | |
self.setLayout(layout) | |
class Controller(QObject): | |
def __init__(self, view): | |
super(Controller, self).__init__() | |
self._view = view | |
self._view.openStuff.connect(self.clicked) | |
# FIXME: Cannot write a decorator like this, no way to have a reference to | |
# `self` because when this is evaluated `self` doesn't fully exist yet. | |
# Remember, decorators are evaluated at definition time! | |
#@self._view.openStuff.connect | |
def clicked(self): | |
print 'Button clicked' | |
if __name__ == "__main__": | |
import sys | |
app = QApplication(sys.argv) | |
view = View() | |
controller = Controller(view) | |
view.show() | |
sys.exit(app.exec_()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment