无法使Qmenu背景弯曲

时间:2019-02-18 17:25:09

标签: python qt user-interface pyqt qmenu

我是pyqt的新手。

我想要一个带有弯曲背景的菜单。但是不能去除黑色边缘。

边框看起来像是弯曲的。但是我无法删除已压缩的背景。

onCreateView

这将显示以下窗口: my menu widget

1 个答案:

答案 0 :(得分:1)

根据需要自定义StyleSheet(CSS)。

import sys
from PyQt5 import QtCore, QtWidgets, QtGui


class main(QtWidgets.QDialog):
    def __init__(self):
        super(main, self).__init__()
        layout = QtWidgets.QVBoxLayout()
        button = QtWidgets.QPushButton("menu", self)
        menu = QtWidgets.QMenu(button)
        menu.setWindowFlags(
            menu.windowFlags() | QtCore.Qt.FramelessWindowHint
        )
        menu.setWindowFlags(
            menu.windowFlags() | QtCore.Qt.NoDropShadowWindowHint
        )
        menu.setFont(QtGui.QFont("Arial", 10, QtGui.QFont.Bold))   # +++

        firstaction  = QtWidgets.QAction(" 1st Item", self)
        secondaction = QtWidgets.QAction(" 2nd Item", self)
        thirdaction  = QtWidgets.QAction(" 3rd Item", self)
        fourthaction = QtWidgets.QAction(" 4th Item", self)

        menu.addAction(firstaction)
        menu.addAction(secondaction)
        menu.addAction(thirdaction)
        menu.addAction(fourthaction)

        button.setMenu(menu)
#        menu.setStyleSheet("""
#        background-color:black;
#        border-radius:20;
#        border:1px solid white;"""
#                           )
        self.setMinimumSize(500, 500)
        layout.addWidget(button)


CSS = """
QWidget {
    background-color: white;  
}
QMenu {
    background-color: #ABABAB; 
    border: 2px solid red;          /* black */
    margin: 0;   
    padding: 5px;
    border-radius: 20px;
    background: white;       
}
QMenu::item {
    background-color: black; 
    color: white; 
    height: 20px;
    width: 60px;
    margin: 1px 0px 1px 0px;
    border: 1px transparent #2A2929;  
    border-radius: 7px;    
}
QMenu::item:selected { 
    background-color: green;  /* #654321; */
}
QMenu::separator {
    height: 12px;
    background: lightblue;
    margin-left: 10px;
    margin-right: 5px;
}
QPushButton {
    background-color: yellow;  
}
""" 

if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    app.setStyleSheet(CSS)                    # +++
    mw = main()
    mw.show()
    sys.exit(app.exec())

enter image description here