Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Language Bindings
  4. [Solved]let the default text disappear when clicked in the QTextEdit’s editable area

[Solved]let the default text disappear when clicked in the QTextEdit’s editable area

Scheduled Pinned Locked Moved Language Bindings
7 Posts 4 Posters 12.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    redstoneleo
    wrote on last edited by
    #1

    The editable area should have “Type code here ” as its text at first ,then when I click the area ,the text should disappear ,so how to implement this functionality ?

    1 Reply Last reply
    0
    • jazzycamelJ Offline
      jazzycamelJ Offline
      jazzycamel
      wrote on last edited by
      #2

      Focus events are your friend ;o) What you're looking for is called place holder text, QLineEdit actually has this, but as far as I know you have to implement it yourself for QTextEdit as follows:

      @
      from PyQt4.QtGui import *

      class TextEdit(QTextEdit):
      def init(self, parent=None):
      QTextEdit.init(self)

          self.setText("Type code here...")
      
      def focusInEvent(self, event):
          self.clear()
          QTextEdit.focusInEvent(self, event)
      

      class Widget(QWidget):
      def init(self, parent=None):
      QWidget.init(self, parent)

          self.setFocus()
      
          l=QVBoxLayout(self)
          l.addWidget(TextEdit(self))
      

      if name=="main":
      from sys import argv, exit
      a=QApplication(argv)
      w=Widget()
      w.show()
      w.raise_()
      exit(a.exec_())
      @

      Note: the "self.setFocus()" in the main widget is just to make sure the QTextEdit doesn't gain focus by default and clear the placeholder text before its even seen ;o)

      For the avoidance of doubt:

      1. All my code samples (C++ or Python) are tested before posting
      2. As of 23/03/20, my Python code is formatted to PEP-8 standards using black from the PSF (https://github.com/psf/black)
      1 Reply Last reply
      0
      • R Offline
        R Offline
        redstoneleo
        wrote on last edited by
        #3

        Does the following code mean reimplement QTextEdit’s focusInEvent method in its subclass ?
        @
        def focusInEvent(self, event):
        QTextEdit.focusInEvent(self, event)
        self.clear()
        @
        in additional ,does the order of
        @ QTextEdit.focusInEvent(self, event)@

        mater ?
        my code is not same as yours :)

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andre
          wrote on last edited by
          #4

          Why so difficult? Simply use the placeholderText property instead...

          1 Reply Last reply
          2
          • jazzycamelJ Offline
            jazzycamelJ Offline
            jazzycamel
            wrote on last edited by
            #5

            bq. Why so difficult? Simply use the placeholderText property instead…

            I'm not aware the QTextEdit has a placeholderText property... it certainly isn't documented "here":http://doc.qt.digia.com/qt/qtextedit.html, unless I'm missing something...

            Anywho, as to redstoneleo's questions:

            • Yes, my example shows a subclass of QTextEdit (simply called TextEdit) in which I re-implement the focusInEvent. This will be called whenever the widget gains focus (either via the mouse, tabbing or programatically).

            • In a simple case like this, the point at which the base class implementation is called probably doesn't matter. However, for more complex cases, if you were wanting to modify some part, but not all, of the base classes behavior the you would call it first. It does depend on the situation.

            For the avoidance of doubt:

            1. All my code samples (C++ or Python) are tested before posting
            2. As of 23/03/20, my Python code is formatted to PEP-8 standards using black from the PSF (https://github.com/psf/black)
            1 Reply Last reply
            0
            • A Offline
              A Offline
              andre
              wrote on last edited by
              #6

              [quote author="jazzycamel" date="1352023509"]bq. Why so difficult? Simply use the placeholderText property instead…

              I'm not aware the QTextEdit has a placeholderText property... it certainly isn't documented "here":http://doc.qt.digia.com/qt/qtextedit.html, unless I'm missing something...[/quote]
              You are right, I apologize. That property is only available on the [[doc:QLineEdit]] widget, not on QTextEdit.

              1 Reply Last reply
              0
              • D Offline
                D Offline
                dbanerjee13
                wrote on last edited by
                #7

                Sorry for hijacking this post. But I really need some help
                This is the code which I use to control my .ui file

                So there is this TextEdit field which by Default shows "Enter your email" but it must go blank when the user clicks on it.
                I am not able to understand how to incorporate the above code.
                Please help!

                import sys
                from PyQt4 import QtCore, QtGui, uic
                from Email import encrypt_email
                from Email import decrypt_email
                
                qtCreatorFile = "rsegui.ui" # Enter file here.
                
                Ui_MainWindow, QtBaseClass = uic.loadUiType(qtCreatorFile)
                
                class MyApp(QtGui.QMainWindow, Ui_MainWindow):
                    def __init__(self):
                        QtGui.QMainWindow.__init__(self)
                        Ui_MainWindow.__init__(self)
                        self.setupUi(self)
                        self.encrypt_and_send.clicked.connect(self.EncryptEmail)
                        self.decrypt.clicked.connect(self.DecryptEmail)
                
                    def focusInEvent(self, event):
                        self.clear()
                        QTextEdit.focusInEvent(self, event)
                
                    def newuser_cleartext(self):
                        self.newuser.clear()
                    def EncryptEmail(self):
                        user=str(self.newuser.toPlainText())
                        sender = str(self.sender.toPlainText())
                        receiver = str(self.receiver.toPlainText())
                        receivers = receiver.split(';')
                        subject = str(self.subject.toPlainText())
                        message = str(self.message.toPlainText())
                        password = str(self.password.toPlainText())
                        encrypt_email(user,sender,receivers,subject,message,password)
                
                    def DecryptEmail(self):
                        email = str(self.sender_2.toPlainText())
                        message = str(self.message_2.toPlainText())
                        self.decrypted.setText(decrypt_email(email,message))
                
                if __name__ == "__main__":
                    app = QtGui.QApplication(sys.argv)
                    window = MyApp()
                    window.show()
                    sys.exit(app.exec_())
                
                
                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved