【问题标题】:How to set the PlaceHolderText for QTextEdit如何为 QTextEdit 设置 PlaceHolderText
【发布时间】:2012-11-12 17:07:26
【问题描述】:

我想设置QTextEdit 的占位符文本。我知道如何为QLineEdit 设置它,有一个属性setPlaceHolderText。但此属性不适用于QTextEdit。请提出您宝贵的建议来解决这个问题。

【问题讨论】:

标签: qt4 qtextedit qt4.7


【解决方案1】:

使用 QTextEdit 的setTextCursor(QTextCursor&) 函数。使用以下逻辑。

  QTextCursor textCursor;
  textCursor.setPosistion(0, QTextCursor::MoveAnchor); 
  textedit->setTextCursor( textCursor );

【讨论】:

  • 你是怎么做到的?
【解决方案2】:

从 Qt 5.3 开始,添加了该属性,因此您现在只需调用setPlaceholderText

【讨论】:

    【解决方案3】:

    我可以通过继承和覆盖绘画事件来做到这一点:

    class PlainTextEditWithPlaceholderText(QtGui.QPlainTextEdit):
        def __init__(self, parent=None):
            super(PlainTextEditWithPlaceholderText, self).__init__(parent)
            self.placeholderText = ""  # Qt-style camelCase
    
        def setPlaceholderText(self, text):
            self.placeholderText = text
    
        def paintEvent(self, _event):
            """
            Implements the same behavior as QLineEdit's setPlaceholderText()
            Draw the placeholder text when there is no text entered and the widget 
            doesn't have focus.
            """
            if self.placeholderText and not self.hasFocus() and not self.toPlainText():
                painter = QtGui.QPainter(self.viewport())
    
                color = self.palette().text().color()
                color.setAlpha(128)
                painter.setPen(color)
    
                painter.drawText(self.geometry().topLeft(), self.placeholderText)
    
            else:
                super(PlainTextEditWithPlaceholderText, self).paintEvent(event)
    

    【讨论】:

    • 我不得不添加self.viewport().update() 并稍微重新定位文本,但除此之外效果很好!谢谢!
    【解决方案4】:

    我发现answer by Rafe 有点缺乏,因为它无法正确格式化文本。不过,从jpo38's answer,我在 Qt5 中找到了它的源代码,并在 Python 中重新实现了我能做的。

    它有一两个变化,但总体上看起来效果很好,它将文本放在正确的位置并支持使用\n 换行。

    注意:这是在 PySide 中使用 Qt.py 进行测试的,如果不使用该文件,则需要将 QtWidgets 重新映射回 QtGui

    class QPlainTextEdit(QtWidgets.QPlainTextEdit):
        """QPlainTextEdit with placeholder text option.
        Reimplemented from the C++ code used in Qt5.
        """
        def __init__(self, *args, **kwargs):
            super(QPlainTextEdit, self).__init__(*args, **kwargs)
    
            self._placeholderText = ''
            self._placeholderVisible = False
            self.textChanged.connect(self.placeholderVisible)
    
        def placeholderVisible(self):
            """Return if the placeholder text is visible, and force update if required."""
            placeholderCurrentlyVisible = self._placeholderVisible
            self._placeholderVisible = self._placeholderText and self.document().isEmpty() and not self.hasFocus()
            if self._placeholderVisible != placeholderCurrentlyVisible:
                self.viewport().update()
            return self._placeholderVisible
    
        def placeholderText(self):
            """Return text used as a placeholder."""
            return self._placeholderText
    
        def setPlaceholderText(self, text):
            """Set text to use as a placeholder."""
            self._placeholderText = text
            if self.document().isEmpty():
                self.viewport().update()
    
        def paintEvent(self, event):
            """Override the paint event to add the placeholder text."""
            if self.placeholderVisible():
                painter = QtGui.QPainter(self.viewport())
                colour = self.palette().text().color()
                colour.setAlpha(128)
                painter.setPen(colour)
                painter.setClipRect(self.rect())
                margin = self.document().documentMargin()
                textRect = self.viewport().rect().adjusted(margin, margin, 0, 0)
                painter.drawText(textRect, QtCore.Qt.AlignTop | QtCore.Qt.TextWordWrap, self.placeholderText())
            super(QPlainTextEdit, self).paintEvent(event)
    

    如果您需要将文本保留到您开始输入的位置,只需删除 not self.hasFocus() 部分。

    【讨论】:

    • 觉得这很有趣——我需要再做一次,因为我忘记了上次我把代码放在哪里了。我遇到了Rafe 的答案,但效果不太好,所以我向下滚动并看到了这个,真的很高兴有人花时间改进它。我尝试投票,发现这是我自己的答案。
    • 编码以神秘的方式工作。我们很高兴有这样的网站!你让我今天一整天都感觉很好。 :D
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-11
    相关资源
    最近更新 更多