PDA

View Full Version : Grey text in empty QLineEdit



Mefisto
3rd May 2009, 17:10
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

Lykurg
3rd May 2009, 17:32
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:cool:


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:

class MyLineEdit : public QLineEdit
{
MyLineEdit(QWidget *parent = 0) : QLineEdit(parent) {}
// here you can use the protected functions of QLineEdit and change whatever you want...
void setText(const QString &str) {QLineEdit::setText("I_was_here_"+str);}
};

Mefisto
3rd May 2009, 20:32
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)...


------------------------- mainwindow.h ------------------
class mainwindow : public QMainWindow, private Ui::MainWindow {
Q_OBJECT
public:
mainwindow();
userEdit *userEditLineEdit;
public slots:
void sendString();
void clearLog();
void manageDatabase();
};

class userEdit : public QLineEdit
{
public:
userEdit();
void setText(const QString &str) {QLineEdit::setText("I_was_here_"+str);}
};

------------------------- mainwindow.cpp ------------------
mainwindow::mainwindow() {
databaseConnect();
userEdit *userEditLineEdit = new userEdit;
setupUi(this);
}

userEdit::userEdit() {
setObjectName(QString::fromUtf8("userEntry"));
setGeometry(QRect(1, 448, 571, 27));
}

Thanks in advance :)
Jonathan

Lykurg
3rd May 2009, 20:57
in short: look for has-a relationship: e.g. http://www.acm.org/crossroads/xrds1-3/ovp.html.

mabeghin
3rd October 2017, 11:35
Since some revision of Qt, you can use QLineEdit::setPlaceholderText