Basic part of PySide2 -- Application of QMenuBar and QMenu

Basic part of PySide2 (12) -- Application of QMenuBar and QMenu

preface:
What can I learn from reading this article?
   each dialog box can be set with a menu bar, which is the best control for the category management button. Please read this article to learn.

——If you think this is a good blog, I hope you can give a little praise, thank you for your support.

1 create menu bar and add menu items

   we tried to add a menu bar to the main dialog and add menu items to the menu bar.
Example code:

from PySide2.QtWidgets import QApplication, QMainWindow, QMenuBar, QMenu

app = QApplication([])

MainWindow = QMainWindow()

MenuBar = MainWindow.menuBar()                  #Get the menu bar of the main dialog
MenuBar.resize(100, 20)

MenuA = MenuBar.addMenu("MenuA")                #Add menu to MenuBar
MenuA1 = MenuA.addMenu("A1")                    #Menu nested submenu
MenuA1.addAction("A1a")
MenuA1.addAction("A1b")
ActionA2 = MenuA.addAction("A2")                #Menu add Action
MenuB = MenuBar.addMenu("MenuB")
MenuB1 = MenuB.addMenu("B1")
MenuB1.addAction("B1a")
MenuB1.addAction("B1b")
MenuB.addAction("B2")

MainWindow.show()
app.exec_()

   operation result:

The process of    operation is to create QMenuBar for dialog box first, then add QMenu or QAction directly to dialog box, QMenu can continue to nest and add QMenu or QAction, always QMenu is used for selection, and the real click is QAction.

2 other operations on QMenuBar

2.1 add Icon

   similar to other spaces, QMenu can display icons and display text at the same time, but the first level QMenu under QMenuBar will not display text after adding icons.
Example code:

from PySide2.QtWidgets import QApplication, QMainWindow, QMenuBar, QMenu
from PySide2.QtGui import QIcon

app = QApplication([])

MainWindow = QMainWindow()

Icon = QIcon(".\png\Icon.ico")

MenuBar = MainWindow.menuBar()                  #Get the menu bar of the main dialog
MenuBar.resize(100, 20)

MenuA = MenuBar.addMenu("MenuA")                #Add menu to MenuBar, MenuA text will not be displayed
MenuA.setIcon(Icon)
MenuA1 = MenuA.addMenu("A1")                    #Menu nested submenu
MenuA1.setIcon(Icon)
ActionA1a = MenuA1.addAction("A1a")
ActionA1a.setIcon(Icon)
ActionA1b = MenuA1.addAction("A1b")
ActionA1b.setIcon(Icon)
ActionA2 = MenuA.addAction("A2")                #Menu add Action
ActionA2.setIcon(Icon)

MainWindow.show()
app.exec_()

   operation result:

2.2 add divider to QMenu

                      .
Example code:

from PySide2.QtWidgets import QApplication, QMainWindow, QMenuBar, QMenu
from PySide2.QtGui import QIcon

app = QApplication([])

MainWindow = QMainWindow()

Icon = QIcon(".\png\Icon.ico")

MenuBar = MainWindow.menuBar()                  #Get the menu bar of the main dialog
MenuBar.resize(100, 20)

MenuA = MenuBar.addMenu("MenuA")                #Add menu to MenuBar, MenuA text will not be displayed
MenuA.setIcon(Icon)
MenuA1 = MenuA.addMenu("A1")                    #Menu nested submenu
MenuA1.setIcon(Icon)
MenuA.addSeparator()                            #Add divider
ActionA1a = MenuA1.addAction("A1a")
ActionA1a.setIcon(Icon)
MenuA1.addSeparator()                           #Add divider
ActionA1b = MenuA1.addAction("A1b")
ActionA1b.setIcon(Icon)
ActionA1c = MenuA1.addAction("A1c")
ActionA1c.setIcon(Icon)
ActionA2 = MenuA.addAction("A2")                #Menu add Action
ActionA2.setIcon(Icon)

MainWindow.show()
app.exec_()

   operation result:

3 add click event

Note that QMenu only displays branches, and the final selection is QAction. When clicking event is added to QMenu, clicking event will be triggered when any QAction under QMenu is clicked. QAction can also add click events.
Example code:

from PySide2.QtWidgets import QApplication, QMainWindow, QMenuBar, QMenu
from PySide2.QtCore import Slot

app = QApplication([])

MainWindow = QMainWindow()

MenuBar = MainWindow.menuBar()                  #Get the menu bar of the main dialog
MenuBar.resize(100, 20)

MenuA = MenuBar.addMenu("MenuA")                #Add menu to MenuBar
MenuA1 = MenuA.addMenu("A1")                    #Menu nested submenu
ActionA1a = MenuA1.addAction("A1a")             #Menu item add Action
ActionA1b = MenuA1.addAction("A1b")
ActionA2 = MenuA.addAction("A2")

@Slot()
def ClickMenuBar():
    print("Click MenuBar")

@Slot()
def ClickMenuA():
    print("Click MenuA")

@Slot()
def ClickMenuA1():
    print("Click MenuA1")

@Slot()
def ClickMenuA1a():
    print("Click MenuA1a")

@Slot()
def ClickMenuA1b():
    print("Click MenuA1b")

@Slot()
def ClickMenuA2():
    print("Click MenuA2")

MenuBar.triggered.connect(ClickMenuBar)                 #Add click event to menu bar
MenuA.triggered.connect(ClickMenuA)                     #Add click event to menu A
MenuA1.triggered.connect(ClickMenuA1)                   #Add click event to menu A1
ActionA2.triggered.connect(ClickMenuA2)                 #Add click event to ActionA2
ActionA1a.triggered.connect(ClickMenuA1a)               #Add click event to ActionA1a
ActionA1b.triggered.connect(ClickMenuA1b)               #Add click event to ActionA1b

MainWindow.show()
app.exec_()

   operation result:

Notice the sequence of event responses here. When QMenu are nested, it takes priority to respond to the click event of the child layer. Click event and QAction click event under QMenu will respond to QAction click event in priority after QMenu click event.

Posted on Sat, 13 Jun 2020 22:59:48 -0400 by biffjo