Skip to content

Instantly share code, notes, and snippets.

@durden
Created April 4, 2013 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save durden/5310601 to your computer and use it in GitHub Desktop.
Save durden/5310601 to your computer and use it in GitHub Desktop.
Example of where using pyqtSignal.connect as a decorator will NOT work
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