I'm actually developping a small soft using PyQt5 and QtDesigner since few weeks now. I've watched a lot of tutos and looked for many answers on SO but I can't find the one concerning the way to override a QWidget's method using uic.loadUI().
Note that I've simplify as much as I could my testing code to point out precisely where my problem is:
1/ This one does not work, it loads my file.ui correctly but clicking doesn't do anything:
import sys
from PyQt5 import uic
from PyQt5.QtWidgets import QApplication, QWidget
class Window(QWidget):
def __init__(self):
super().__init__() # Or QWidget.__init__(self)
self.dlg = uic.loadUi("file.ui")
self.dlg.show()
def mousePressEvent(self, event):
print("click !")
if __name__ == '__main__':
app = QApplication(sys.argv)
win = Window()
app.exec_()
2/ But I've figured out that that one is actually working:
class Window(QWidget):
def __init__(self):
QWidget.__init__(self)
self.setWindowTitle("My window")
self.show()
def mousePressEvent(self, event):
print("click !")
if __name__ == '__main__':
app = QApplication(sys.argv)
win = Window()
app.exec_()
So as I can see it seems to be because I can't override the event's method. Due to the fact that mousePressEvent is not a method attached to my QWidget subclass self.dlg
.
Now that I know why, I'd like to know how can I override the mousePressEvent method and use it. I was wondering about 'loading' the method after overrided it, or calling the QWidget method to redefining it, or simply create my own method to catch any event, but everything I tried completely failed.
Anyhow, thanks in advance for your answers / tips / prayers.