[PyQt] When to and not to pass parent argument to widget constructors
Florian Bruhin
me at the-compiler.org
Tue Apr 22 16:33:03 BST 2014
Hi Timothy,
first of all, sending plaintext mails instead of HTML mails might be
easier for people, as not everybody uses a GUI mailclient to read
their mails.
* rj_pyqt_qwzqfors <rj_pyqt_qwzqfors at objectmail.com> [2014-04-22 16:43:32 +0200]:
> I am learning PyQt in order to rewrite my wxPython application to
> use it. Having been accustomed to using wxPython, I am used to being
> required to pass a parent as an argument to the constructor of every
> non-top-level widget. But in PyQt, the parent argument is often
> optional, and I am having some difficulty to know when I should and
> shouldn't use it.
>
> From what I have learned so far, I was guessing that perhaps the
> parent argument should not be used in cases where the parent of the
> widget will later be specified by calling a function (such as
> QDockWidget.setWidget, QMainWindow.addDockWidget,
> QMainWindow.addToolBar, QMainWindow.setCentralWidget,
> QStatusBar.addPermanentWidget, QToolBar.addWidget, ,
> QTabWidget.addTab, etc.), but should be used in all other cases. Is
> this correct? Could someone please help clarify this?
That's about how I do it as well. If you don't pass a parent, the
object won't get deleted automatically, and you have to care about
this yourself. If you do pass one, if you delete the parent, its
children will be deleted automatically.
> Also, which is the more acceptable practice:
>
> from PyQt5.QtWidgets import *
>
> OR:
>
> from PyQt5.QtWidgets import QAction,
> QLabel,...QTabWidget, QToolBar
>
> (I know that generally the use of wild imports is discouraged in
> Python, but it's much easier than having to add a widget to the
> import list whenever I use another one.)
I personally prefer to import what I actually need.
My applications are broken down into multiple files, and the longest
import-Line so far is this:
from PyQt5.QtWidgets import (QWidget, QLineEdit, QProgressBar, QLabel,
QHBoxLayout, QStackedLayout, QSizePolicy, QShortcut)
I don't think this is that bad really.
The main argument against "import *" is namespace pollution. In case
of (Py)Qt this isn't such a hard argument, since at least everything
starts with Q* or pyqt*, so clashes are minimized.
Florian
--
() ascii ribbon campaign - stop html mail www.asciiribbon.org
/\ www.the-compiler.org | I love long mails http://email.is-not-s.ms/
Boy, that crayon sure did hurt!
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: not available
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20140422/7ce97eda/attachment.sig>
More information about the PyQt
mailing list