2

Using PyQt5, I'm using a QWidget as my main form (could just as well be a QMainWindow).

I need to do something after Qt decides the size of the main form based on the screen size and other factors, therefore I can't use the constructor, rather I have to override showEvent. For an example of why this is necessary in some cases or for more detail see for example this post: Getting the size of a QGraphicsView

Here is a quick skeleton example:

# test.py

import cv2
import numpy as np

from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QGridLayout
from PyQt5.QtGui import QImage, QPixmap
from PyQt5.Qt import Qt


def main():
    app = QApplication([])
    mainForm = MainForm()
    mainForm.show()
    app.exec()
# end main

class MainForm(QWidget):

    def __init__(self):
        super().__init__()

        self.setGeometry(300, 300, 400, 200)

        self.label = QLabel('hello world')
        self.label.setAlignment(Qt.AlignCenter)

        self.gridLayout = QGridLayout()
        self.gridLayout.addWidget(self.label)
        self.setLayout(self.gridLayout)
    # end function

    def showEvent(self, event):
        print('in showEvent')
        # Set image on QGraphicsView here, or something else that has to be done in showEvent

        # Which of these is correct ??
        super(MainForm, self).showEvent(event)
        super().showEvent(event)
    # end function
# end class

if __name__ == '__main__':
    main()

My questions about how to properly override showEvent specifically are:

  1. Does it matter if super is called? In my limited testing I can't find that it makes a difference for this function specifically.

  2. Should super be called as super(MainForm, self).showEvent(event) or super().showEvent(event) ? I can't find a situation where one works and the other does not, and I've found multiple examples of each way being used. Is there a meaningful difference between these?

  3. If super should be called, should the other functionality (ex. setting the image of a QGraphicsView) be done before or after the call to super? Does it matter, and if so, what are the practical differences that doing other stuff before vs after calling super would cause?

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
cdahms
  • 3,402
  • 10
  • 49
  • 75

1 Answers1

3

1. Does it matter if super is called? In my limited testing I can't find that it makes a difference for this function specifically.

It depends, when you call super().method(args) you are calling the functionality of the parent class, in the case the parent classes implement some functionality in that method and in other cases not for what should be the decision of the developer if He does it or not. If the parent class does not implement any extra functionality then calling it or not will do the same but if it implements some other functionality and you do not call it, the class may not do its job properly.

So in general it is considered good practice to call the parent's super().method(args) method unless you want to override that behavior.

To understand what can happen if you do not call the parent's method you can use the following example:

import sys

from PyQt5 import QtCore, QtWidgets


class LineEdit(QtWidgets.QLineEdit):
    def keyPressEvent(self, event):
        print(event.text())
        # super().keyPressEvent(event)


if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    w = LineEdit()
    w.show()
    sys.exit(app.exec_())

In the following example, to not call the parent's keyPressEvent method, you have disabled the default behavior so the text will not be displayed in the QLineEdit. But if you did the same with QWidget there would be no problems because the keyPressEvent method of that class does not implement anything.

2. Should super be called as super(MainForm, self).showEvent(event) or super().showEvent(event) ? I can't find a situation where one works and the other does not, and I've found multiple examples of each way being used. Is there a meaningful difference between these?

This question has been answered in Understanding Python super() with __init__() methods, both are equivalent in python3.

3. If super should be called, should the other functionality (ex. setting the image of a QGraphicsView) be done before or after the call to super? Does it matter, and if so, what are the practical differences that doing other stuff before vs after calling super would cause?

It depends. It will depend on understanding what the parent method does, for example if you want your extra functionality to run before or after the default behavior.


In your particular case since you are overriding the showEvent method of MainForm that inherits directly from QWidget that does nothing according to the source code it does not matter if you call the showEvent method (although it is recommended that you call super() as good practice), it does not matter either that you add the functionality before or after super.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241