How to set the PlaceHolderText for QTextEdit

假装没事ソ 提交于 2020-01-03 17:43:30

问题


I just want to set a PlaceHolderText for a QTextEdit. I know how to set it for a QLineEdit. There is a Property, setPlaceHolderText for QLineEdit. But this Property is not available for QTextEdit. Please give your valuable suggestions to solve this.


回答1:


Use setTextCursor(QTextCursor&) function of QTextEdit. Use the following logic.

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



回答2:


I was able to do this by subclassing and overriding the paint event:

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)



回答3:


Since Qt 5.3, The property was added, so you now simply need to call setPlaceholderText




回答4:


I found the answer by Rafe to be a little lacking as it was unable to correctly format the text. From jpo38's answer though, I found the source code of it in Qt5 and re-implemented what I could in Python.

It's got one or two changes, but overall seems to work nicely, it puts the text in the correct place and supports using \n for new lines.

Note: This is tested in PySide with Qt.py, if not using that file, you will need to remap QtWidgets back to 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)

If you need the text to remain up until the point you start typing, just remove the not self.hasFocus() part.



来源:https://stackoverflow.com/questions/13348117/how-to-set-the-placeholdertext-for-qtextedit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!