Thomas Perl wrote:
> Ohai!
>
> 2011/1/17 Fars- mo :
> > Wouldn't it be plain beauty to be able to write a one-line hello world?
> >
> > with QtGui.QApplication: QtGui.QLabel("Hello world!").show()
>
> The one-liner wouldn't work, because the QLabel will be destroyed
> after show() was called (because it drops out of context - you have to
> keep a reference to it for it to stay alive).

I forgot to mention this in the mailing list, but I recently found out that all 
the enhancements in this thread are dependent on the fact that this behavior 
gets changed.

I filed this as a bug, although because it's controversial and a pretty big 
change it will likely become a PSEP:
http://bugs.openbossa.org/show_bug.cgi?id=619


> This can be done with a simple context manager, actually:
>
> ============
> from contextlib import contextmanager
> import sys
>
> from PySide.QtGui import *
>
> @contextmanager
> def QtApp():
> app = QApplication(sys.argv)
> yield app
> sys.exit(app.exec_())
> ============
>
> Usage:
>
> with QtApp():
> l = QLabel("Hello world!")
> l.show()
>
> Or also with access to the QApplication object should the need arise:
>
> with QtApp() as app:
> w = QMainWindow()
> w.setWindowTitle('blubb')
> pb = QPushButton('heya')
> pb.clicked.connect(lambda: app.exit(2))
> w.setCentralWidget(pb)
> w.show()

No, your examples would fall prey to the same issue as the one-liner because if 
I'm not mistaken, l and w would have ended their lifetimes when the context 
manager's __exit__ is called.


Cheers,
Farsmo
                                          
_______________________________________________
PySide mailing list
PySide@lists.openbossa.org
http://lists.openbossa.org/listinfo/pyside

Reply via email to