6

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

4

4 回答 4

3

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

  QTextCursor textCursor;
  textCursor.setPosistion(0, QTextCursor::MoveAnchor); 
  textedit->setTextCursor( textCursor );
于 2012-12-11T17:02:16.147 回答
3

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

于 2017-12-21T13:26:01.993 回答
2

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

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

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

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()部分即可。

于 2019-02-06T12:25:51.093 回答
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)
于 2017-05-10T01:07:59.420 回答