Некорректное отображение закруглённой рамки(border-radius) в Qt.ActionsContextMenu
Собственно. На лейблах и контекстном меню у меня один и тот же styleSheet. Однако в контекстном меню закругление работает не совсем корректно.
Если это возможно, то желательно без qt.customcontextmenu.
import sys, os
from PyQt5 import QtWidgets
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QMovie, QPixmap
from PyQt5.QtWidgets import *
class MyWin(QWidget):
def __init__(self):
super().__init__()
label = QtWidgets.QLabel(self)
label.resize(300, 300)
label.setStyleSheet('''background-color: grey;
padding: 5;
border: 1px solid #f09ea3;
border-radius: 10px;
''')
label.setContextMenuPolicy(Qt.ActionsContextMenu)
action = QtWidgets.QAction('text', self)
label.addAction(action)
self.setAttribute(Qt.WA_TranslucentBackground, True)
self.setWindowFlags(Qt.WindowStaysOnTopHint | Qt.FramelessWindowHint)
self.resize(label.width(), label.height())
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
w = MyWin()
w.show()
Ответы (1 шт):
Автор решения: S. Nick
→ Ссылка
Как вариант. Проверено на Windows.
import sys
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QIcon, QFont
from PyQt5.QtWidgets import QApplication, QLabel, QMenu, QAction, QWidget
class MyWin(QWidget):
def __init__(self):
super().__init__()
self._createActions() # +++
self.label = QLabel("Hello, World", self, font=QFont("Calibri", 20))
self.label.setAlignment(Qt.AlignCenter)
self.label.resize(300, 300)
self.label.setStyleSheet('''background-color: grey;
border: 2px solid #f00;
border-radius: 10px;
''')
self.setAttribute(Qt.WA_TranslucentBackground, True)
self.setWindowFlags(Qt.FramelessWindowHint)
self.resize(self.label.width(), self.label.height())
# +++ vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
self._menu = QMenu()
self._menu.setWindowFlags(Qt.FramelessWindowHint)
self._menu.setAttribute(Qt.WA_TranslucentBackground)
self._menu.setStyleSheet("""
QMenu{
background-color: rgb(255, 255, 255);
border-radius: 10px;
border: 2px solid red;
}
QMenu::item {
background-color: transparent;
padding:3px 20px;
margin:5px 10px;
border-radius: 5px;
}
QMenu::item:selected { background-color: gray; }
""")
newAct = self._menu.addAction(self.newAction)
openAct = self._menu.addAction(self.openAction)
self._menu.addSeparator()
quitAct = self._menu.addAction(self.exitAction)
QApplication.instance().focusObjectChanged.connect(self.focusChanged)
self._connectActions()
def _createActions(self):
self.newAction = QAction(self)
self.newAction.setText("&New")
self.newAction.setIcon(QIcon("img/new.png"))
self.openAction = QAction(QIcon("img/open.png"), "&Open...", self)
self.exitAction = QAction(QIcon("img/exit.png"), "&Quit", self)
def focusChanged(self, obj):
if not obj or obj.window() == self.window():
self._menu.hide()
def contextMenuEvent(self, event):
self._menu.hide()
action = self._menu.exec_(self.mapToGlobal(event.pos()))
def _connectActions(self):
self.newAction.triggered.connect(self.newFile)
self.openAction.triggered.connect(self.openFile)
self.exitAction.triggered.connect(self.close)
def newFile(self):
self.label.setText("<b>File > <i style='color: red'>New...</i></b> clicked")
self._menu.hide()
def openFile(self):
self.label.setText("<b>File > <i style='color: red'>Open...</i></b> clicked")
self._menu.hide()
# +++ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
if __name__ == "__main__":
app = QApplication(sys.argv)
w = MyWin()
w.show()
sys.exit(app.exec_())

