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):
            super().__init__()
            self.ui = Ui_MainWindow()
            self.ui.setupUi(self)
            self.ui.pushButtonEnter.clicked.connect(self.dispmessage)
            self.ui.lineEditAnswer.setText("Enter something here")
            self.ui.lineEditAnswer.mousePressEvent(self)
            self.ui.labelVocab.setText("null")
            self.show()
    
        def dispmessage(self):
            self.ui.labelVocab.setText(self.ui.lineEditAnswer.text())
    
    if __name__ == "__main__":
        deck = parser.parser()
        app = QApplication(sys.argv)
        win = MainWindow()
        win.show()
        sys.exit(app.exec_())```


  • 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