简体   繁体   English

QTextEdit 角不遵循样式表中指定的边框边界

[英]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:最后,一些重要的考虑因素:

  • stylesheets should be always used with care, as they should usually be considered for special "exceptions" only, as their behavior might be erratic;应始终小心使用样式表,因为它们通常只应考虑用于特殊的“例外”,因为它们的行为可能不稳定;
  • using large values for border radius is not suggested, as it might clip other contents (most importantly, the scroll bars);不建议使用大的边框半径值,因为它可能会剪辑其他内容(最重要的是,滚动条);
  • some styles apply the stylesheet parameters in different ways, especially with QAbstractScrollArea subclasses (like QTextEdit);一些样式以不同的方式应用样式表参数,尤其是 QAbstractScrollArea 子类(如 QTextEdit); for example, with Oxygen style, i get the rounded border inside the text area contents, while I don't get it in the Windows style;例如,与氧的风格,我得到的文本区域里面的内容圆角的边框,而我没有在Windows风格得到它;
  • it's usually good practice to use specific syntax in stylesheets, meaning that you should specify the widget class (which is important for widgets containing subwidgets like QTextEdit) and provide correct indentation:在样式表中使用特定语法通常是一种很好的做法,这意味着您应该指定小部件(这对于包含 QTextEdit 等子小部件的小部件很重要)并提供正确的缩进:
        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.

相关问题 在 Python 2.7 中分配 styleObj 后,如何使用 sheet['A'].style/styleObj 分配样式? - How to assign a style using sheet['A'].style/styleObj after styleObj has been assigned in Python 2.7? hist和axvline不遵循颜色循环/ matplotlib样式 - hist and axvline do not follow color cycle / matplotlib style 到达字段边界时,QTextEdit不会生成新行 - QTextEdit doesn't make new line when reaching field border 无法更改 Pyqt5 中 qtextedit 的背景边框,有什么建议吗? - can't change backgrund border of qtextedit in Pyqt5, any suggestions? python matplotlib给出了错误的边框角落 - python matplotlib gives wrong corners of border python不遵循重定向 - python do not follow redirect 我正在用 Pygame 制作游戏,尽管我指定了边界,但我的玩家正在拐角处 - I'm making a game with Pygame, my player is going of the corners though i specified the boundaries 我如何使用pyqt5中的QTextEdit来显示html的所有样式(包括css的样式) - how I use the QTextEdit in pyqt5 to show all style of the html (including the style of css) Pygame 圆角按钮,border_radius 参数不起作用 - Pygame Button with rounded Corners, border_radius argument does not work 如何在 kivy 中使我的边框图像的角更圆 - How to make the corners of my border image rounder in kivy
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM