Search code examples
pythonpyqtpyqt4qmenuqfont

How to change QMenu font size


The code below creates a Menu with 5 Submenus and 10 Actions per each Submenu. Even while the setPointSize command is applied to the Submenus their font seem to be unaffected and it remains to be large. But the Actions font is set to a smaller size even while the command is performed on Submenus and not the Actions. How to change the font size for both the Submenus and Actions?

enter image description here

from PyQt5.QtWidgets import QMenu, QApplication
app = QApplication([])

menu = QMenu()
for i in range(5):
    submenu = menu.addMenu('Submenu %04d' % i)
    font = submenu.font()
    font.setPointSize(10)
    submenu.setFont(font)
    for n in range(10):
        action = submenu.addAction('Action %04d' % n)

menu.show()
app.exec_()

Solution

  • You must apply the font to all the menu as shown below:

    from PyQt5.QtWidgets import QMenu, QApplication
    app = QApplication([])
    
    menu = QMenu()
    font = menu.font()
    font.setPointSize(18)
    menu.setFont(font)
    for i in range(5):
        submenu = menu.addMenu('Submenu %04d' % i)
        submenu.setFont(font)
        for n in range(10):
            action = submenu.addAction('Action %04d' % n)
    
    menu.show()
    app.exec_()
    

    enter image description here