Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

The ContextMenu and the sublevels

I am able to create a ContextMenu with a level and a first sublevel. I was trying to add more sublevels but I can’t. I searched the Internet for a long time to find some examples, but nothing. In my example, how should I modify the code to add the ‘Brenda’ option in the second sublevel? Please look at the picture. Thanks.

ContextMenu Image

def contextMenuEvent(self,event):

    menu = QMenu(self)

    Option1 = menu.addAction("Paul")

    Option2 = QMenu("Richard")
    Option2_1 = Option2.addAction("Frida")
    Option2_2 = Option2.addAction("Susan")
    menu.addMenu(Option2)

    Option3 = menu.addAction("Thomas")

    action = menu.exec_(self.mapToGlobal(event.pos()))
    if action == Option1:
        print("Paul")
    elif action == Option2_1:
        print("Frida")
    elif action == Option2_2:
        print("Susan")
    elif action == Option3:
        print("Thomas")

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Shouldn’t this be how to achieve a third menu layer?

def contextMenuEvent(self,event):

    menu = QMenu(self)

    Option1 = menu.addAction("Paul")

    Option2 = QMenu("Richard")
    Option2_1 = Option2.addAction("Frida")
    Option2_2 = QMenu("Susan") # define a new menu, not an action
    Option2_2_1 = Option2_2.addAction("Brenda") # add sub menu item
    Option2.addMenu(Option2_2) # add menu to Option2 menu
    menu.addMenu(Option2)

    Option3 = menu.addAction("Thomas")

    action = menu.exec_(self.mapToGlobal(event.pos()))
    if action == Option1:
        print("Paul")
    elif action == Option2_1:
        print("Frida")
    elif action == Option2_2:
        print("Susan")
    elif action == Option3:
        print("Thomas")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading