繁体   English   中英

有没有办法在 PyQt5 中设置 QTextEdit.placeholderText() 的 alignment?

[英]Is there a way to set the alignment of the QTextEdit.placeholderText() in PyQt5?

我知道您可以使用setAlignment()设置文本的 alignment ,但这对占位符文本没有影响。 也许您需要编辑基础documentstyleSheet表才能做到这一点? 还是文档仅与实际文本相关而不与占位符相关?

这是一个可以玩的MWE:

import sys

from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QDialog, QApplication, QGridLayout, QTextEdit


class Test(QDialog):
    def __init__(self):
        super().__init__()
        self.edit = QTextEdit()
        self.edit.setPlaceholderText(
            # '<html><body><p align="center">'
            'this is a placeholder'
            # '</p></body></html>'
        )
        self.edit.setAlignment(Qt.AlignHCenter)
        self.lay = QGridLayout(self)
        self.lay.addWidget(self.edit)
        self.setLayout(self.lay)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    GUI = Test()
    GUI.show()
    sys.exit(app.exec_())

占位符不能直接自定义,因为它是由 QTextEdit 直接使用默认顶部 alignment 绘制的,因此唯一的解决方案是子类化,覆盖paintEvent并使用您自己的 alignment 绘制占位符。

您还可以通过使用 QTextDocument 添加更多控制,这将允许您使用 html 和自定义颜色/对齐/等。

html占位符文本

class HtmlPlaceholderTextEdit(QTextEdit):
    _placeholderText = ''
    def setPlaceholderText(self, text):
        if Qt.mightBeRichText(text):
            self._placeholderText = QTextDocument()
            try:
                color = self.palette().placeholderText().color()
            except:
                # for Qt < 5.12
                color = self.palette().windowText().color()
                color.setAlpha(128)
            # IMPORTANT: the default stylesheet _MUST_ be set *before*
            # adding any text, otherwise it won't be used.
            self._placeholderText.setDefaultStyleSheet(
                '''body {{color: rgba({}, {}, {}, {});}}
                '''.format(*color.getRgb()))
            self._placeholderText.setHtml(text)
        else:
            self._placeholderText = text
        self.update()

    def placeholderText(self):
        return self._placeholderText

    def paintEvent(self, event):
        super().paintEvent(event)
        if self.document().isEmpty() and self.placeholderText():
            qp = QPainter(self.viewport())
            margin = self.document().documentMargin()
            r = QRectF(self.viewport().rect()).adjusted(margin, margin, -margin, -margin)
            text = self.placeholderText()
            if isinstance(text, str):
                try:
                    color = self.palette().placeholderText().color()
                except:
                    # for Qt < 5.12
                    color = self.palette().windowText().color()
                    color.setAlpha(128)
                qp.setPen(color)
                qp.drawText(r, self.alignment() | Qt.TextWordWrap, text)
            else:
                text.setPageSize(self.document().pageSize())
                text.drawContents(qp, r)


class Test(QDialog):
    def __init__(self):
        super().__init__()
        self.edit = HtmlPlaceholderTextEdit()
        self.edit.setPlaceholderText(
            '<html><body><p align="center">'
            'this is a <font color="blue">placeholder</font>'
            '</p></body></html>'
        )
        # ...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM