英文:
How to make round edges for the main window in PyQt?
问题
问题非常简单,但我目前无法成功:
**在 PyQt/PySide 代码中,制作主窗口的圆角的最简单方法是什么?** 我尝试了很多在线找到的方法,但都不起作用。以下是最简单的示例(我认为应该起作用,但实际上并没有):
from PySide6.QtWidgets import QApplication, QMainWindow
import sys
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setStyleSheet("border-radius: 10px;")
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MainWindow()
sys.exit(app.exec())
英文:
The question is extremely simple, but I am not succeeding at it at the moment:
What is the simplest way to make round edges to the main window in PyQt/PySide code? I have tried lots of things that can be found online but none works. Here is th simplest example (that is supposed to work in my opinion, but doesn't):
from PySide6.QtWidgets import QApplication, QMainWindow
import sys
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setStyleSheet("border-radius: 10px;")
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MainWindow()
sys.exit(app.exec())
答案1
得分: 3
不可能将QSS(Qt样式表)的border-radius属性应用于QMainWindow。
[evidence1] Qt样式表参考|Qt Widgets 6.4.2
请参阅属性列表章节中的border-radius属性。border-radius属性不支持QtMainWindow。
https://doc.qt.io/qt-6/stylesheet-reference.html#brush
> 此属性受到QAbstractItemView子类、QAbstractSpinBox子类、QCheckBox、QComboBox、QFrame、QGroupBox、QLabel、QLineEdit、QMenu、QMenuBar、QPushButton、QRadioButton、QSplitter、QTextEdit和QToolTip的支持。
[evidence2] Qt论坛|如何使QMainWindow的边角变圆?
作为参考,Qt论坛也发布了border-radius无法应用于顶层窗口(QMainWindow)的信息。
https://forum.qt.io/topic/127952/how-is-it-possible-to-make-rounded-edges-for-qmainwindow/2
[备选提案]
如果QWidget不缺少您需要的功能,可以使用它来替代QMainWindow。以下代码创建了一个圆形QWidget。
from PySide6.QtWidgets import QApplication, QMainWindow
from PySide6.QtWidgets import QWidget
from PySide6.QtCore import Qt
import sys
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
w = 200
h = 100
# 主窗口
self.resize(w, h)
# 移除边框
self.setWindowFlag(Qt.FramelessWindowHint)
# 使主窗口透明
self.setAttribute(Qt.WA_TranslucentBackground)
# 圆形小部件
self.round_widget = QWidget(self)
self.round_widget.resize(w, h)
self.round_widget.setStyleSheet(
"""
background:rgb(255, 255, 255);
border-radius: 50px;
"""
)
#self.setStyleSheet("border-radius: 10px;".format(radius))
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MainWindow()
sys.exit(app.exec())
这段代码创建了以下窗口。
英文:
It is not possible to apply the border-radius property of QSS (Qt style Sheets) to QMainWindow.
[evidence1]Qt Style Sheet Reference|Qt Widgets 6.4.2
See the border-radius in List of Properties chapter. border-radius property doesn't support QtMainWindow.
https://doc.qt.io/qt-6/stylesheet-reference.html#brush
> This property is supported by QAbstractItemView subclasses, QAbstractSpinBox subclasses, QCheckBox, QComboBox, QFrame, QGroupBox, QLabel, QLineEdit, QMenu, QMenuBar, QPushButton, QRadioButton, QSplitter, QTextEdit, and QToolTip.
[evidence2]Qt Forum|How is it possible to make rounded edges for QMainWindow?
For reference, Qt Forum also posted that border-radius cannot be applied to QSS in the top level window (QMainWindow).
https://forum.qt.io/topic/127952/how-is-it-possible-to-make-rounded-edges-for-qmainwindow/2
[Alternative proposal]
If QWidget doesn't lack the functionality you need, you can use it instead of QMainWindow. The following code creates a round QWidget.
from PySide6.QtWidgets import QApplication, QMainWindow
from PySide6.QtWidgets import QWidget
from PySide6.QtCore import Qt
import sys
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
w = 200
h = 100
#main window
self.resize(w, h)
#remove frame
self.setWindowFlag(Qt.FramelessWindowHint)
#make the main window transparent
self.setAttribute(Qt.WA_TranslucentBackground)
#round widget
self.round_widget = QWidget(self)
self.round_widget.resize(w, h)
self.round_widget.setStyleSheet(
"""
background:rgb(255, 255, 255);
border-radius: 50px;
"""
)
#self.setStyleSheet("border-radius: 10px;".format(radius))
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MainWindow()
sys.exit(app.exec())
The code creates following window.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论