PDA

View Full Version : clear all QLineEdit



jaca
13th May 2008, 14:26
Hello!
Is there any way to clean the QLineEdits with little code?
Thanks.

mitro
13th May 2008, 16:47
Have you tried lineEdit->setText("") ;)?

momesana
13th May 2008, 16:53
QLineEdit::clear()
That question actually belongs into the Newbie forum and you should always take a look at the qt documentation before asking ;).
If I misunderstood your question and you are speaking of Many QLineEditS that you want to clear at once, you should have a function that iterates through the list of QLineEdits and calls the mentioned function on them. Use findChildren for that:


foreach(QLineEdit* le, p->findChildren<QLineEdit*>()) {
le->clear();
}
where p is the common parentwidget/ancestor of all the lineedits.

jaca
13th May 2008, 17:01
Yes, better use lineEdit.clear(). But I have many lineEdits to clear and the code would be great. I wanted to know if there is a loop for this.

jaca
13th May 2008, 17:46
was easier to use without declaring P. It was thus:


foreach(QLineEdit* le, findChildren<QLineEdit*>()) {
le->clear();
}

momesana
14th May 2008, 01:15
was easier to use without declaring P.
Yes, the code above works fine if all Line edits are children or descendants (in terms of parenthood, not inheritance) of the Widget you are calling that function from. In that case the implicit "this" corresponds to p as findChildren() is the same as this->findChildren(). However, if the line edits happen to be in a widget that is not a descendant/child of "this" then you must explicitly specify their ancestor/parent :).

yeyeVonel09
5th August 2016, 03:25
foreach(QLineEdit* le, findChildren<QLineEdit*>()) {
le->clear();
}

this code is working and thank you.
but i have some QLineEdit I don't need to clear.
what should be the code?

MasterBLB
5th August 2016, 07:06
QLineEdit::clear() is a slot,so you can connect an own-defined signal clearLineEdits to those line edits you want to be cleared at once:


//prefferably in constructor:
Class::Class()
{
connect(this, SIGNAL(clearLineEdits()), lineEdit_nr1, SLOT(clear()));
.......
connect(this, SIGNAL(clearLineEdits()), lineEdit_nrX, SLOT(clear()));
}

and then if emit the signal somewhere in your code:


emit clearLineEdits();


Why not simpy a function where you'd put all the line edits to clear?Because signal-slot approach is more flexible,as you can add/remove line edits during runtime.