[Solved]let the default text disappear when clicked in the QTextEdit’s editable area
-
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 ?
-
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)
-
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 :) -
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.
-
-
[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. -
Sorry for hijacking this post. But I really need some help
This is the code which I use to control my .ui fileSo 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_())