Results 1 to 5 of 5

Thread: Grey text in empty QLineEdit

  1. #1
    Join Date
    Mar 2009
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Grey text in empty QLineEdit

    Hello!

    First i have to admit that I'm a complete beginner to QT. I'm writing many small apps to learn QT by doing; that's working very nice, if I don't know something I'm looking in the documentation. For the most problems, this works and has much more effect to me than stupidly read the documentation from the top to the end. But now I'm facing a problem with QLineEdit and I don't have the slightest idea how to solve it. I've already searched in the docu but I didn't find anything really usefull. So heres the problem:

    I have a QLineEdit field, in which the user should be able to enter his or her name. The field should be empty after starting the program. But because i don't want to label the field, i would like to insert a light-grey text which says "Enter your name here, please." into the field. The text should disappear just after the user entered anything. That means if the User enters the first letter of his or her name, the letter he/she tiped should be in the field instead of the grey text. If the filed is empty (the user deletes every character), the grey text should appear again.

    I've already asked in the freenode #qt channel and a member told me to subclass the lineEdit-field. And that's the problem: I don't really know what is meant with subclass. It would be very nice if someone could send me a link where i can read about subclassing; I know what a class is and how to use them.

    Thanks in advance!!!
    Jonathan

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Grey text in empty QLineEdit

    Quote Originally Posted by Mefisto View Post
    Hello!

    First i have to admit that I'm a complete beginner to QT.
    Then there is a Newbie section in this board...
    I'm writing many small apps to learn QT by doing; that's working very nice, if I don't know something I'm looking in the documentation. For the most problems, this works and has much more effect to me than stupidly read the documentation from the top to the end. But now I'm facing a problem with QLineEdit and I don't have the slightest idea how to solve it. I've already searched in the docu but I didn't find anything really usefull. So heres the problem:
    that's a good way to learn.
    I have a QLineEdit field, in which the user should be able to enter his or her name. The field should be empty after starting the program. But because i don't want to label the field, i would like to insert a light-grey text which says "Enter your name here, please." into the field. The text should disappear just after the user entered anything. That means if the User enters the first letter of his or her name, the letter he/she tiped should be in the field instead of the grey text. If the filed is empty (the user deletes every character), the grey text should appear again.
    easy
    I've already asked in the freenode #qt channel and a member told me to subclass the lineEdit-field. And that's the problem: I don't really know what is meant with subclass. It would be very nice if someone could send me a link where i can read about subclassing; I know what a class is and how to use them.
    Ask google! lot's of hints. If you figured out, see also focusIn and focusOut event. You will need them, and because I am kind subclass is:
    Qt Code:
    1. class MyLineEdit : public QLineEdit
    2. {
    3. MyLineEdit(QWidget *parent = 0) : QLineEdit(parent) {}
    4. // here you can use the protected functions of QLineEdit and change whatever you want...
    5. void setText(const QString &str) {QLineEdit::setText("I_was_here_"+str);}
    6. };
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Mar 2009
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Grey text in empty QLineEdit

    Hm I still have some problems... I didn't find anything via google, so I looked in the QT-Examples. I didn't find anything usefull there, too. Here's what i've got right now, I think a lot is wrong but I don't see the error(s)...

    Qt Code:
    1. ------------------------- mainwindow.h ------------------
    2. class mainwindow : public QMainWindow, private Ui::MainWindow {
    3. Q_OBJECT
    4. public:
    5. mainwindow();
    6. userEdit *userEditLineEdit;
    7. public slots:
    8. void sendString();
    9. void clearLog();
    10. void manageDatabase();
    11. };
    12.  
    13. class userEdit : public QLineEdit
    14. {
    15. public:
    16. userEdit();
    17. void setText(const QString &str) {QLineEdit::setText("I_was_here_"+str);}
    18. };
    19.  
    20. ------------------------- mainwindow.cpp ------------------
    21. mainwindow::mainwindow() {
    22. databaseConnect();
    23. userEdit *userEditLineEdit = new userEdit;
    24. setupUi(this);
    25. }
    26.  
    27. userEdit::userEdit() {
    28. setObjectName(QString::fromUtf8("userEntry"));
    29. setGeometry(QRect(1, 448, 571, 27));
    30. }
    To copy to clipboard, switch view to plain text mode 

    Thanks in advance
    Jonathan

  4. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Grey text in empty QLineEdit

    in short: look for has-a relationship: e.g. http://www.acm.org/crossroads/xrds1-3/ovp.html.

  5. #5
    Join Date
    Dec 2014
    Posts
    1
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X

    Default Re: Grey text in empty QLineEdit

    Since some revision of Qt, you can use QLineEdit::setPlaceholderText

Similar Threads

  1. Unhandled exception in qatomic
    By NewGuy in forum Qt Programming
    Replies: 14
    Last Post: 23rd July 2013, 09:49
  2. Default delegate displays empty QLineEdit?
    By andy.fillebrown in forum Qt Programming
    Replies: 3
    Last Post: 16th April 2009, 13:13
  3. QLineEdit and background text
    By SnarlCat in forum Qt Programming
    Replies: 2
    Last Post: 6th March 2009, 22:17
  4. QLineEdit text() crash - Qt3
    By user_mail07 in forum Qt Programming
    Replies: 7
    Last Post: 10th June 2008, 09:42
  5. Pointer Question related to QLineEdit
    By ChrisReath in forum Qt Programming
    Replies: 1
    Last Post: 23rd May 2008, 15:13

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.