如何设置的QTextEdit的PlaceHolderText

问题描述 投票:4回答:4

我只是想设置一个QTextEdit一个PlaceHolderText。我知道如何设置一个QLineEdit的。有一个属性,setPlaceHolderText为QLineEdit的。但是,这财产是不可用于的QTextEdit。请给您的宝贵建议,以解决这个问题。

qt4 placeholder qt4.7 qtextedit qlineedit
4个回答
3
投票

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

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

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)

1
投票

由于Qt的5.3,加入的属性,所以你现在只需要调用setPlaceholderText


0
投票

我发现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()部分。

© www.soinside.com 2019 - 2024. All rights reserved.