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:
Does it matter if
super
is called? In my limited testing I can't find that it makes a difference for this function specifically.Should super be called as
super(MainForm, self).showEvent(event)
orsuper().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?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?