0

self.centralwidget = QtWidgets.QWidget(MainWindow) Why is MainWindow passed to QtWidget? It was generate using QtDesigner btw.. Any ideas?

  • 1
    Because it sets `MainWindow` as the parent of `self.centralwidget`? – mkrieger1 Oct 05 '21 at 15:49
  • 1
    This might answer your question: https://stackoverflow.com/questions/30354166/what-is-parent-for-in-qt – mkrieger1 Oct 05 '21 at 15:51
  • If you look at the [QWidget()](https://doc.qt.io/qt-5/qwidget.html#QWidget) constructor in the documentation, you'll see that the first (keyworded, as in *optional*) argument is the *parent* widget. Creating a widget with a parent ensures that it will always be a child of *that* parent and will only be drawn (*clipped*) inside that parent's coordinate system (although this might not happen for specific cases, such as QDialog). If no parent is given, the widget becomes a *top level* widget (it has its own window). Also read [Object Tree and Ownership](https://doc.qt.io/qt-5/objecttrees.html). – musicamante Oct 05 '21 at 15:56
  • Just in case, a very important aspect to remember: reading `pyuic` generated files is educational, as it allows to better understand how widgets *can be* created in PyQt. But, please, keep in mind that those files should never, ***EVER*** be modified (if not ***exclusively*** for learning purposes), nor you should try to mimic their behavior or subclass their classes. Those files should always be imported *as they are* in the main scripts of your program, as explained in the official guidelines about [using Designer](https://www.riverbankcomputing.com/static/Docs/PyQt5/designer.html). – musicamante Oct 05 '21 at 16:01

0 Answers0