Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved] How to set the PlaceHolderText for a TextEdit?
Forum Updated to NodeBB v4.3 + New Features

[Solved] How to set the PlaceHolderText for a TextEdit?

Scheduled Pinned Locked Moved General and Desktop
16 Posts 4 Posters 27.8k Views 1 Watching
  • 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.
  • A Offline
    A Offline
    AlekseyOk
    wrote on last edited by
    #4
    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
    1 Reply Last reply
    0
    • M Offline
      M Offline
      Macro
      wrote on last edited by
      #5

      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

      1 Reply Last reply
      0
      • A Offline
        A Offline
        AlekseyOk
        wrote on last edited by
        #6

        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;
        }
        }
        @

        1 Reply Last reply
        0
        • J Offline
          J Offline
          JoyRider
          wrote on last edited by
          #7

          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.

          1 Reply Last reply
          0
          • M Offline
            M Offline
            Macro
            wrote on last edited by
            #8

            [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

            1 Reply Last reply
            0
            • M Offline
              M Offline
              Macro
              wrote on last edited by
              #9

              [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

              1 Reply Last reply
              0
              • A Offline
                A Offline
                AlekseyOk
                wrote on last edited by
                #10

                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

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

                  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();
                  }
                  @

                  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
                  • M Offline
                    M Offline
                    Macro
                    wrote on last edited by
                    #12

                    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...

                    1 Reply Last reply
                    0
                    • M Offline
                      M Offline
                      Macro
                      wrote on last edited by
                      #13

                      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

                      1 Reply Last reply
                      0
                      • A Offline
                        A Offline
                        AlekseyOk
                        wrote on last edited by
                        #14

                        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
                        1 Reply Last reply
                        0
                        • jazzycamelJ Offline
                          jazzycamelJ Offline
                          jazzycamel
                          wrote on last edited by
                          #15

                          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();
                          }
                          @

                          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
                          • M Offline
                            M Offline
                            Macro
                            wrote on last edited by
                            #16

                            Thanks a lot jazzycamel.. :-)

                            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