[Python] PyQt5 Styling QMenu Dropdown?

Discussão em 'Python' iniciado por Stack, Abril 24, 2025.

  1. Stack

    Stack Membro Participativo

    I made a QMenuBar with a QMenu with two actions. However I can't figure out how to style the dropdown that appears.

    My attempt to style the dropdown just isn't working. From what I can tell, QAction can't be given styles, so I tried QMenu but that doesn't work either. So I'm not sure how to style this dropdown...

    OS: Windows 11 Using PyQt5

    from PyQt5.QtWidgets import QApplication, QWidget, QMenuBar, QMenu, QAction, QVBoxLayout, QMainWindow

    class test(QMainWindow):
    def __init__(self):
    super(test, self).__init__()

    self.setWindowTitle('test')
    self.resize(200,200)

    self.mainWindow = QWidget()

    self.mainLayout = QVBoxLayout()

    self.bar = QMenuBar()
    self.menuItem = QMenu('Help')

    self.bar.addMenu(self.menuItem)

    self.menuAction = QAction('Action')

    self.menuItem.addAction(self.menuAction)

    self.mainLayout.addWidget(self.bar)

    self.mainWindow.setStyleSheet("""
    QWidget {
    background-color: #222;
    color: #cfcfcf;
    font-family: Roboto;
    font-size: 14pt;
    }

    QMenuBar {
    background-color: #222;
    padding-top: 4px;
    }

    QMenuBar::item {
    border-radius: 4px;
    border-width: 0;
    padding: 4px 8px;
    }

    QMenuBar::item:selected {
    background-color: #777777;
    }

    QMenu, QAction {
    background-color: red;
    border-radius: 4px;
    border-width: 0;
    }

    QMenu::item {
    color: blue;
    }

    QMenu::item:selected {
    background-color: aqua;
    color: orange;
    }
    """)
    self.mainWindow.setLayout(self.mainLayout)
    self.setCentralWidget(self.mainWindow)

    if __name__ in '__main__':
    app = QApplication([])
    main = test()
    main.show()
    app.exec_()


    [​IMG]

    Continue reading...

Compartilhe esta Página