[英]QTextEdit corners do not follow the border boundaries assigned in style sheet
How to correct sharp corners of a QTextEdit
widget applied in style sheet ?如何更正样式表中应用的
QTextEdit
小部件的尖角?
Corners of the QTextEdit
being inside the border 's boundaries is the purpose. QTextEdit
角落在border的边界内是目的。
# importing libraries
from PySide2.QtWidgets import *
from PySide2.QtGui import *
from PySide2.QtCore import *
import sys
class Scene(QGraphicsScene):
def __init__(self, _scene_width, _scene_height):
super(Scene, self).__init__()
self.setSceneRect(0, 0, _scene_width, _scene_height)
map_center = _scene_width / 2
te = QTextEdit()
te.setStyleSheet(" border-radius: 30px;background-clip: border; background-color: rgb(220, 220,220); color: rgb(113, 113, 113); font: 14pt Vazir; border:4px solid; border-color: rgb(255,95,95);")
te.insertPlainText("Test Text")
te.setGeometry(map_center, map_center, 100, 100)
self.addWidget(te)
class View(QGraphicsView):
def __init__(self, _scene_width, _scene_height):
super(View, self).__init__()
self.scale_factor = 1
self.scene_width = _scene_width
self.scene_height = _scene_height
scene = Scene(_scene_width, _scene_height)
self.setScene(scene)
self.showMaximized()
def main():
app = QApplication(sys.argv)
view = View(1000, 1000)
view.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
Result of the code代码结果
Adding widgets to a graphics scene makes things a bit more complicated.将小部件添加到图形场景会使事情变得更加复杂。
The problem is that widgets without any parent are treated as top level widgets ("windows") are: even if you set a rounded border around them, the widget will still have a rectangular shape, and that shape will always be drawn using the background role palette of the widget, including what's outside the rounded edges.问题在于没有任何父级的小部件被视为顶级小部件(“窗口”)是:即使您在它们周围设置了圆形边框,小部件仍将具有矩形形状,并且该形状将始终使用背景绘制小部件的角色调色板,包括圆形边缘之外的内容。
Since you are using stylesheets, one thing that can be done is to set the background palette role to transparent
(this works since you're specifying the background in the stylesheet and that background will be clipped to the border):由于您使用的是样式表,因此可以做的一件事是将背景调色板角色设置为
transparent
(这是有效的,因为您在样式表中指定背景并且该背景将被剪切到边框):
palette = te.palette()
palette.setBrush(palette.Window, QBrush(Qt.transparent))
te.setPalette(palette)
Note that this might not be enough, as some styles (notably, the Oxygen style) add a default background alpha gradient to the palette while painting the widget, and there's almost nothing you can do about that, at least in this situations.请注意,这可能还不够,因为某些样式(特别是 Oxygen 样式)在绘制小部件时向调色板添加了默认的背景 alpha 渐变,并且您对此几乎无能为力,至少在这种情况下是这样。
In that case, as with top level windows, you need to set a mask on the widget.在这种情况下,与顶级窗口一样,您需要在小部件上设置掩码。 Consider that masks are bit maps, meaning that no aliasing is applied: the pixels are drawn or they are not.
考虑到掩码是位图,这意味着没有应用混叠:像素被绘制或不绘制。
mask = QPainterPath()
# the rectangle needs a 1 pixel adjustment to ensure that the aliasing is
# correctly drawn, with the drawback of showing partial background pixels
mask.addRoundedRect(QRectF(te.rect()).adjusted(-1, -1, 1, 1), 30, 30)
te.setMask(QRegion(mask.toFillPolygon(QTransform()).toPolygon()))
Finally, some important considerations:最后,一些重要的考虑因素:
te.setStyleSheet('''
QTextEdit {
border-radius: 30px;
background-clip: border;
background-color: rgb(220, 220,220);
color: rgb(113, 113, 113);
font: 14pt Vazir;
border:4px solid;
border-color: rgb(255,95,95);
}
''')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.