Please nominate your Qt Champions for 2021!

Clear QTextEdit widget on mouse click in text entry box

  • Hi all, I'm using PyQt5 and I'm trying to get the QTextBox to clear itself when it is clicked with mouse (or tabbed into, for that matter). I am not sure how to accomplish this but I was looking at the method mousePressEvent(), but I guess this would only cover the mouse press instead of having tabbed into it as well

    import sys
    import parser
    from PyQt5.QtWidgets import QDialog, QApplication
    from PyQt5.Qt import QEvent, QMouseEvent, QWidget
    from MainWindow import *
    class MainWindow(QtWidgets.QMainWindow):
        def __init__(self):
            self.ui = Ui_MainWindow()
            self.ui.lineEditAnswer.setText("Enter something here")
        def dispmessage(self):
    if __name__ == "__main__":
        deck = parser.parser()
        app = QApplication(sys.argv)
        win = MainWindow()

  • I solved my own problem. I wanted to make it so that I had "Enter your answer" in the text input until the user actually entered something.. BUT you can just select this in QDesigner QTextEdit properties and select 'placeholderText' to properly show a greyed out message prior to input.

Log in to reply