簡體   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