如何为QTextEdit设置PlaceHolder文本

时间:2012-11-12 17:07:26

标签: qt4 placeholder qt4.7 qtextedit qlineedit

我只想为 QTextEdit 设置 PlaceHolderText 。我知道如何为 QLineEdit 设置它。 QLineEdit 有一个属性 setPlaceHolderText 。但是此属性不适用于QTextEdit。请提出宝贵的建议来解决这个问题。

4 个答案:

答案 0 :(得分:3)

使用QTextEdit的setTextCursor(QTextCursor&)功能。使用以下逻辑。

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

答案 1 :(得分:2)

我能够通过子类化和覆盖paint事件来实现这一点:

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)

答案 2 :(得分:1)

自Qt 5.3起,该属性已添加,因此您现在只需要调用setPlaceholderText

答案 3 :(得分:0)

我发现Rafe的答案有点不足,因为它无法正确设置文本格式。不过,从jpo38的答案中,我在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()部分即可。