gpt4 book ai didi

python - FramelessWindowhint 导致 qmenu 无法正常工作

转载 作者:太空宇宙 更新时间:2023-11-03 21:15:41 25 4
gpt4 key购买 nike

我是 pyqt 新手。我想摆脱 qmenu 影子。但是当我添加framelesswindowhint时,菜单无法正常工作。

实现framelesswindowhint之前的代码:

import sys
from PyQt5 import QtWidgets, QtCore, QtGui


class MainWindow(QtWidgets.QWidget):
def __init__(self):
super(MainWindow, self).__init__()
self.setMinimumSize(800, 400)
self.setStyleSheet(
"""
background-color:white;
"""
)
self.setWindowFlags(QtCore.Qt.FramelessWindowHint)
self.mainlayout = QtWidgets.QVBoxLayout()

button = QtWidgets.QPushButton("my button")
filemenu = QtWidgets.QMenu(button)
#filemenu.setWindowFlags(QtCore.Qt.FramelessWindowHint)
filemenu.setObjectName("filemenu")
openaction = QtWidgets.QAction("open item", self)
filemenu.addAction(openaction)
button.setMenu(filemenu)
self.mainlayout.addWidget(button)
self.setLayout(self.mainlayout)


if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
# app.setStyleSheet(qdarkstyle.load_stylesheet_pyqt5())
mw = MainWindow()
# mw.setWindowOpacity(0.95)
mw.show()
sys.exit(app.exec_())

输出:(单击按钮): enter image description here

取消注释该行后:(按钮未单击) enter image description here

(单击按钮) enter image description here

最佳答案

Qt.NoDropShadowWindowHint 在支持平台上禁用窗口阴影。

import sys
from PyQt5 import QtWidgets, QtCore, QtGui


class MainWindow(QtWidgets.QWidget):
def __init__(self):
super(MainWindow, self).__init__()
self.setWindowFlags(QtCore.Qt.FramelessWindowHint)

self.label = QtWidgets.QLabel(self, alignment=QtCore.Qt.AlignCenter)
button = QtWidgets.QPushButton("my button")

filemenu = QtWidgets.QMenu(button)
filemenu.triggered.connect(self.selected)

#filemenu.setWindowFlags(QtCore.Qt.FramelessWindowHint) # ---
filemenu.setWindowFlags(filemenu.windowFlags() # +++
| QtCore.Qt.NoDropShadowWindowHint) # +++

openaction = QtWidgets.QAction("open item", self)
filemenu.addAction(openaction)
filemenu.addAction("Do something...")

button.setMenu(filemenu)

self.mainlayout = QtWidgets.QVBoxLayout()
self.mainlayout.addStretch()
self.mainlayout.addWidget(self.label)
self.mainlayout.addWidget(button)
self.mainlayout.addStretch()
self.setLayout(self.mainlayout)

def selected(self, q):
self.label.setText(q.text() + ' selected')


CSS = """
QWidget {
background-color: white;
}

QMenu {
background-color: #ABABAB; /* sets background of the menu */
border: 1px solid black;
margin: 0;
}

QMenu::item {
background-color: red;
}

QMenu::item:selected {
background-color: #654321;
}

QMenu::separator {
height: 12px;
background: lightblue;
margin-left: 10px;
margin-right: 5px;
}

QLabel {
color: red;
font: 27px;
}

QPushButton {
background-color: yellow;
}
"""

if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
# app.setStyleSheet(qdarkstyle.load_stylesheet_pyqt5())
app.setStyleSheet(CSS)
mw = MainWindow()
# mw.setWindowOpacity(0.95)
mw.setMinimumSize(800, 400)
mw.show()
sys.exit(app.exec_())

enter image description here

关于python - FramelessWindowhint 导致 qmenu 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54734810/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com