小编ale*_*lec的帖子

QLabel 正确定位文本轮廓

我正在尝试创建一个带有文本轮廓的标签。我只想要一个带有黑色轮廓的简单白色文本。我首先尝试在 css 中这样做,就像这样label.setStyleSheet("color:white; outline:2px black;")
\n但是大纲没有\xe2\x80\x99 做任何事情。

\n

我做了很多搜索并找到了使用 qpainter 路径来完成此操作的方法。但问题是文本总是被切断。在此输入图像描述

\n

根据该功能,文本应该从左下角开始,但它开始得太低且太左。我知道我可以通过尝试错误找到一个点,这样它就不会\xe2\x80\x99t 被切断——你可以-20 到高度,对于这个来说就足够了。但它只会修复这个特定的文本!对于任何具有不同大小、文本或字体的标签来说,它都不会相同。

\n

我将在这里放置最小的代码示例

\n
from PyQt5 import QtCore, QtGui, QtWidgets\nfrom PyQt5.QtWidgets import QWidget, QLabel, QMainWindow\n\n\nclass MainLabel(QLabel):\n    def __init__(self, text):\n        super(MainLabel, self).__init__(text)\n\n    def paintEvent(self, event):\n        qp = QtGui.QPainter()\n        qp.begin(self)\n        qp.setRenderHint(QtGui.QPainter.Antialiasing)\n        font=QtGui.QFont()\n        font.setPointSize(70)\n        painterPath = QtGui.QPainterPath()\n        #how to get the right positioning for addText\n        painterPath.addText(0, self.height(), font,self.text())#HERE\n        qp.strokePath(painterPath, QtGui.QPen(QtGui.QColor(0,0,0), 6))\n        qp.fillPath(painterPath, QtGui.QColor(255,255,255))\n        qp.end()\n\n\nclass MainWindow(QMainWindow):\n    def __init__(self):\n        super(MainWindow, self).__init__()\n        self.centralWidget=QWidget(self)\n        self.setCentralWidget(self.centralWidget)\n        self.lay = QtWidgets.QVBoxLayout()\n        self.centralWidget.setLayout(self.lay)\n        self.label = MainLabel("text …
Run Code Online (Sandbox Code Playgroud)

python outline pyqt5

2
推荐指数
1
解决办法
2669
查看次数

标签 统计

outline ×1

pyqt5 ×1

python ×1