Show More
@@ -208,6 +208,55 b' Rules:' | |||||
208 | the Kernel has been shutdown. |
|
208 | the Kernel has been shutdown. | |
209 | * Remote Consoles may not restart or shutdown the kernel. |
|
209 | * Remote Consoles may not restart or shutdown the kernel. | |
210 |
|
210 | |||
|
211 | Qt and the QtConsole | |||
|
212 | ==================== | |||
|
213 | ||||
|
214 | An important part of working with the QtConsole when you are writing your own Qt code is | |||
|
215 | to remember that user code (in the kernel) is *not* in the same process as the frontend. | |||
|
216 | This means that there is not necessarily any Qt code running in the kernel, and under most | |||
|
217 | normal circumstances there isn't. If, however, you specify ``pylab=qt`` at the | |||
|
218 | command-line, then there *will* be a :class:`QCoreApplication` instance running in the | |||
|
219 | kernel process along with user-code. To get a reference to this application, do: | |||
|
220 | ||||
|
221 | .. sourcecode:: python | |||
|
222 | ||||
|
223 | from PyQt4 import QtCore | |||
|
224 | app = QtCore.QCoreApplication.instance() | |||
|
225 | # app will be None if there is no such instance | |||
|
226 | ||||
|
227 | A common problem listed in the PyQt4 Gotchas_ is the fact that Python's garbage collection | |||
|
228 | will destroy Qt objects (Windows, etc.) once there is no longer a Python reference to | |||
|
229 | them, so you have to hold on to them. For instance, in: | |||
|
230 | ||||
|
231 | .. sourcecode:: python | |||
|
232 | ||||
|
233 | def make_window(): | |||
|
234 | win = QtGui.QMainWindow() | |||
|
235 | ||||
|
236 | def make_and_return_window(): | |||
|
237 | win = QtGui.QMainWindow() | |||
|
238 | return win | |||
|
239 | ||||
|
240 | :func:`make_window` will never draw a window, because garbage collection will destroy it | |||
|
241 | before it is drawn, whereas :func:`make_and_return_window` lets the caller decide when the | |||
|
242 | window object should be destroyed. If, as a developer, you know that you always want your | |||
|
243 | objects to last as long as the process, you can attach them to the QApplication instance | |||
|
244 | itself: | |||
|
245 | ||||
|
246 | .. sourcecode:: python | |||
|
247 | ||||
|
248 | # do this just once: | |||
|
249 | app = QtCore.QCoreApplication.instance() | |||
|
250 | app.references = set() | |||
|
251 | # then when you create Windows, add them to the set | |||
|
252 | def make_window(): | |||
|
253 | win = QtGui.QMainWindow() | |||
|
254 | app.references.add(win) | |||
|
255 | ||||
|
256 | Now the QApplication itself holds a reference to ``win``, so it will never be | |||
|
257 | garbage collected until the application itself is destroyed. | |||
|
258 | ||||
|
259 | .. _Gotchas: http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/gotchas.html#garbage-collection | |||
211 |
|
260 | |||
212 | Regressions |
|
261 | Regressions | |
213 | =========== |
|
262 | =========== |
General Comments 0
You need to be logged in to leave comments.
Login now