[Solved] How to set the PlaceHolderText for a TextEdit?
-
Hi Developers,
Can anyone Please suggest me how to set the PlaceHolderText for a QTextEdit. QLineEdit has a Property setPlaceHolderText, But why the QTextEdit didn't have that? If i want to set a PlaceHolderText for TextEdit, How can i set this?
Thanks & Regards
-
You can reimplement virtual function paintEvent():
@
void YourTextEditClass::paintEvent(QPaintEvent *pe)
{
if (text().isEmpty())
{
QPainter p(this);
p.drawText(geometry(), Qt::AlignHCenter | Qt::AlignVCenter, "PlaceHolder");
}
else
{
QTextEdit::paintEvent(pe);
}
}
@ -
Hi Aleksey Okinchitc...
Sorry i couldn't get the exact requirement, i can't able to check the QTextEdit's text is empty or not. How to check that one, In that if condition what is that text() refers to?
I Tried it like this
@void TextEdit::paintEvent(QPaintEvent *event)
{
if(ui->textEdit->text()->isEmpty() // Here i can't get the text() function for my QTextEdit.QPainter paint(this); paint.drawText(geometry(), Qt::AlignHCenter | Qt::AlignVCenter, "PlaceHolder");
}@
-
- Subclass YourTextEditClass from QTextEdit
- I've posted source code for subclassed QTextEdit
- If you want to use painteEvent() outside from YourTextEditClass you should call "installEventFilter()":http://qt-project.org/doc/qt-4.8/qobject.html#installEventFilter first.
- Why you can't call a text() method?
- I would suggest you use subclassing from QTextEdit
-
Yeah thanks for your suggestion Aleksey Okinchitc.
I didn't subclassed my TextEdit. I am just using it directly from the Ui Designer. But am not sure about the reason why that ui->TextEdit->text() is not called.
Also, I have an another idea, But am not sure whether it will work or not. Why can't we use a focus in event for setting the Place Holder Text.???
Please correct me if i am going wrong.
Thanks & Regards
-
Yes, I forgot about foucsInEvent, you should check for it and AND for an empty text.
You can see an example in Qt sources (gui/widgets/qlineedit.cpp, paintEvent())@
if (d->control->text().isEmpty()) {
if (!hasFocus() && !d->placeholderText.isEmpty()) {
QColor col = pal.text().color();
col.setAlpha(128);
QPen oldpen = p.pen();
p.setPen(col);
lineRect.adjust(minLB, 0, 0, 0);
QString elidedText = fm.elidedText(d->placeholderText, Qt::ElideRight, lineRect.width());
p.drawText(lineRect, va, elidedText);
p.setPen(oldpen);
return;
}
}
@ -
Hello Aleksey Okinchitc & Riz,
First of all Thanks a lot for your posts guys. Today only i noticed that there is no PlaceHolderText for a TextEdit. can you guys please post the complete source code for it. Because I'm a newbie to Qt. I Tried to solve it by myself. But i failed. So pleasel help me for my learning.
-
[quote author="Aleksey Okinchitc" date="1353314575"]1. Subclass YourTextEditClass from QTextEdit
2. I've posted source code for subclassed QTextEdit
3. If you want to use painteEvent() outside from YourTextEditClass you should call "installEventFilter()":http://qt-project.org/doc/qt-4.8/qobject.html#installEventFilter first.
4. Why you can't call a text() method?
5. I would suggest you use subclassing from QTextEdit[/quote]Hi Aleksey Okinchitc,
I tried to do it as you said above, but even after SubClassing my QTextEdit, I couldn't get that text() function for my QTextEdit. I'm getting only the TextColor ( ), TextCursor ( ), TextChanged ( ) and the TextBackgroundColor ( ) functions only. So what i can do? Is there any other way of doing it ?
Thanks & Regards
-
[quote author="Joy Rider" date="1353398758"]Hello Aleksey Okinchitc & Riz,
First of all Thanks a lot for your posts guys. Today only i noticed that there is no PlaceHolderText for a TextEdit. can you guys please post the complete source code for it. Because I'm a newbie to Qt. I Tried to solve it by myself. But i failed. So pleasel help me for my learning. [/quote]
Hi Joy Rider,
Welcome to DevNet Forum....
That's good atleast today u noticed that there is no setPlaceHolderText for QTextEdit... :-)
I had not yet completed my coding. will Update it once i had completed it.
Thanks & Regards
-
Yes, my apologize. I've wrote an example of general idea :-)).
Instead of text() function you should use something like "QTextEdit::toPlainText()":http://qt-project.org/doc/qt-4.8/qtextedit.html#plainText-prop -
I posted something similar to this recently ("here":http://qt-project.org/forums/viewthread/21474/ in the "Language Bindings" forum) written in Python. I thought I'd post a (slightly expanded) C++ version in the hope it might be of use to someone:
@
#include <QtGui>
#include <QDebug>class TextEdit : public QTextEdit {
Q_PROPERTY(QString placeholderText READ placeholderText WRITE setPlaceholderText)
public:
TextEdit(QWidget *parent=0) : QTextEdit(parent) {}void setPlaceholderText(QString text){ placeholderText=text; if(toPlainText().isEmpty()) setText(placeholderText); }
protected:
void focusInEvent(QFocusEvent *e){
if (!placeholderText.isNull()){
QString t=toPlainText();
if (t.isEmpty() || t==placeholderText) clear();
}
QTextEdit::focusInEvent(e);
}void focusOutEvent(QFocusEvent *e){ if (!placeholderText.isNull()){ if (toPlainText().isEmpty()) setText(placeholderText); } QTextEdit::focusOutEvent(e); }
private:
QString placeholderText;
};class Widget : public QWidget {
public:
Widget(QWidget *parent=0) : QWidget(parent) {
QVBoxLayout *l=new QVBoxLayout(this);TextEdit *t1=new TextEdit(this); t1->setPlaceholderText("Type here..."); l->addWidget(t1); TextEdit *t2=new TextEdit(this); t2->setPlaceholderText("or here..."); l->addWidget(t2); setFocus(); }
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
@ -
Hi jazzycamel...
Thanks a lot.. This is what i required.. It Works Fine.. Once again a special Thanks to all for your valuable suggestions.. :-)
Thanks & Regards...
-
Hi Guys....
I just have a small doubt. I want the PlaceHolderText of the QTextEdit should be like a Disabled Text like QLineEdit's placeHolderText. Is there any easy way of doing it or whether we have to use the Paint Event for this to paint the PlaceHolderText as a disabled color text?
Thanks & Regards
-
You can try to use mechanism of pseudo-state in Style sheets.
Pseudo-code:
- pseudo state ["EmptyText_ShowPlaceHolder"]: color #FF0000
- pseudo state ["NonEmptyText_or_Focused_DoNotShowPlaceHolder"]: color #000000
-
QTextEdit supports rich text so just replace "setText" at lines 11 and 23 with "setHtml" and style as desired. The following sets the placeholder text to be light grey and italic:
@
#include <QtGui>
#include <QDebug>class TextEdit : public QTextEdit {
Q_PROPERTY(QString placeholderText READ placeholderText WRITE setPlaceholderText)
public:
TextEdit(QWidget *parent=0) : QTextEdit(parent) {}void setPlaceholderText(QString text){ placeholderText=text; if(toPlainText().isEmpty()) setHtml(QString("<font color=\"#808080\"><i>%1</i></font>").arg(placeholderText)); }
protected:
void focusInEvent(QFocusEvent *e){
if (!placeholderText.isNull()){
QString t=toPlainText();
if (t.isEmpty() || t==placeholderText) clear();
}
QTextEdit::focusInEvent(e);
}void focusOutEvent(QFocusEvent *e){ if (!placeholderText.isNull()){ if (toPlainText().isEmpty()) setHtml(QString("<font color=\"#808080\"><i>%1</i></font>").arg(placeholderText)); } QTextEdit::focusOutEvent(e); }
private:
QString placeholderText;
};class Widget : public QWidget {
public:
Widget(QWidget *parent=0) : QWidget(parent) {
QVBoxLayout *l=new QVBoxLayout(this);TextEdit *t1=new TextEdit(this); t1->setPlaceholderText("Type here..."); l->addWidget(t1); TextEdit *t2=new TextEdit(this); t2->setPlaceholderText("or here..."); l->addWidget(t2); setFocus(); }
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
@ -
Thanks a lot jazzycamel.. :-)