How Do I Use Signals From A Qwidget To Tell The Main Window To Execute A Function?
I am trying to run a function inputAttendance(AthleteInfo) whenever the enter button is pressed on the confirmPopup window. This function is contained within another file that I im
Solution 1:
Helloww sir, that's a really nice question, let's summarize in this way.
- Let's suppose we have two classes, the Widget class and the MainWindow class.
- The widget will contain two signals, one signal runs a method from itself, the other signal will run a method from the MainWindow.
- The first signal we will connect inside the widget, emit and run a method from the Widget.
- The second will be connected inside the MainWindow where we have a widget instance of our class and will run a method from the MainWindow
Let's have a look in this small example that represents what I just tried to summarize.
from PyQt5.QtCore import pyqtSignal
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWidgets import QMainWindow
from PyQt5.QtWidgets import QWidget
classMainWindow(QMainWindow):
myWidget = Nonedef__init__(self):
super(MainWindow, self).__init__()
self.myWidget = Widget()
self.setFixedSize(500,500)
self.myWidget.setFixedSize(500,500)
self.layout().addWidget(self.myWidget)
self.myWidget.signalExecuteAMainWindowFunction.connect(self.mainWindowFunction)
defmainWindowFunction(self):
print("running my MAINWINDOW function...")
classWidget(QWidget):
signalExecuteMyFunction = pyqtSignal()
signalExecuteAMainWindowFunction = pyqtSignal()
def__init__(self):
super(Widget, self).__init__()
self.signalExecuteMyFunction.connect(self.myFunction)
defmousePressEvent(self, QMouseEvent):
self.signalExecuteMyFunction.emit()
super(Widget, self).mousePressEvent(QMouseEvent)
defmouseMoveEvent(self, QMouseEvent):
self.signalExecuteAMainWindowFunction.emit()
super(Widget, self).mouseMoveEvent(QMouseEvent)
defmyFunction(self):
print("running my WIDGET function...")
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
main_window = MainWindow()
main_window.show()
sys.exit(app.exec_())
Ps: There are many more beautiful ways to do some things that I've done, like variable names, object orientation and organization, but it's the essence of what you asked that matter. Hope it clarified a little bit more about how it works. :D
Post a Comment for "How Do I Use Signals From A Qwidget To Tell The Main Window To Execute A Function?"