How to set validator for all Line edit
-
I have two tasks,
Task 1: I have two group box . Now i want to set validator for the line edit which is present in the group box based on the type of group box its present.
Eg: one group box with three line edit only gets integer value and the other group box only gets string value
Task 2: Based on the group box i want to set validator for all the line edit present in group box.
Eg: one group box has 3 line edit then i have to set validator in common for all the line edit so that it validates all the line edit data .
Is it possible ?
-
It is possible. What is possible is - Setting the validator is per object. You need to find the logic to set the validator for each&every lineEdit objects.
-
can we set the validator for a individual group instead of setting for each and every line edit.
Set Validator for all line edit in group which accepts only int values instead of setting for each and every line edit can we set validator for a all line edit at once so that it works.
Set Validator for all line edit in group which accepts only string values .
-
No nothing exist like this.
-
Hi,
You can use findChildren and use a loop to set the validator.
Also, you might want to consider using a more appropriate widget for integer input like QSpinBox.
-
for group box i did something like this and it woked
QList<QLineEdit *> allPButtons = ui->groupBox- >findChildren<QLineEdit *>(); for (int i=0; i < allPButtons.size(); i++) { allPButtons.at(i)->setValidator(new QIntValidator(0,20,this)); }
-
Yes, it possible if with little bit more programming. Set the object name for each of the lineEdit object. Inside the for loop just do something like.
for (int i=0; i < allPButtons.size(); i++) { if(allPButtons.at(i).objectName.compare("ManiRon")==0) { // set validator }else { allPButtons.at(i)->setValidator(new QIntValidator(0,20,this)); } }
-
@dheerendra said in How to set validator for all Line edit:
if(allPButtons.at(i).objectName.compare("ManiRon")==0) {
// set validator
}elseit worked thanks sir
-
Cool. Enjoy Qt programming. Please move the issue to SOLVED state. It helps others as well.
-
One small thing:
@ManiRon said in How to set validator for all Line edit:QList<QLineEdit *> allPButtons
Be more careful with the names of your variables.
allPButtons
suggests a list of QPushButton which is unrelated to your QLineEdit setup. This makes your code hard to read and understand.