Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is the function launched without a function call?

Can somebody explain to me how the program knows that if I click the right mouse button it must launch the contextMenuEvent? Where is this call made? And if it is the parent class that is doing it, how it knows the name of the function?

import sys
from PyQt5.QtWidgets import QMainWindow, qApp, QMenu, QApplication

class Example(QMainWindow):

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

        self.initUI()


    def initUI(self):         

        self.setGeometry(300, 300, 300, 200)
        self.setWindowTitle('Context menu')    
        self.show()


    def contextMenuEvent(self, event):

           cmenu = QMenu(self)

           newAct = cmenu.addAction("New")
           opnAct = cmenu.addAction("Open")
           quitAct = cmenu.addAction("Quit")
           action = cmenu.exec_(self.mapToGlobal(event.pos()))

           if action == quitAct:
               qApp.quit()

if __name__ == '__main__': 
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())
like image 396
Igor Dragushhak Avatar asked Jul 28 '26 18:07

Igor Dragushhak


1 Answers

QMainWindow already provides the contextMenuEvent method which is called by the Qt framework.

In your class, you are overriding this method so that your method is called instead of the one from QMainWindow.

See also

  • QMainWindow::contextMenuEvent()
  • Qt Documentation: Menus Example
like image 177
Andreas Fester Avatar answered Jul 30 '26 10:07

Andreas Fester



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!