PDA

View Full Version : How do i create a boxchecker?



Terreox
1st February 2012, 15:44
Hi
i would like to know how i can check some lineedit's content on button click?
i chose the clicked() Event of a button and now i want to create a function to check if something is in the dialog's lineedits or not.
i want to have a separate function that i call if the event is triggered.
how can i solve this?

KillGabio
1st February 2012, 16:13
Hi terreox,
I generally do that with a lineEdit and a Checkbox i put right next to it (like buddies -> http://www.youtube.com/watch?v=2edb0VOkx-k&list=PL2D1942A4688E9D63&index=6&feature=plpp_video). I disable the checkbox as checkable so that the user cannot check the info. The events I use are if return clicked or (I think this is going to be more useful to you) editing finished, this triggers when the focus on the lineEdit is lost. I ll give you an example:



void Formulario::on_lineEdit_2_editingFinished()
{
int x = this->ui->lineEdit_2->text ().length ();
if ( x< 50 && x> 3){
this->ui->checkBox_2->setChecked (true);
this->ui->checkBox_2->setIcon (QIcon ());
}else{
this->ui->checkBox_2->setIcon (QIcon (":ImagesMilk/Iconos/25.png"));
this->ui->checkBox_2->setChecked (false);
}
}


tell me if this helps you

Added after 4 minutes:

Here is an example of the form that goes with that code. So you start typing in a line edit you press Tab for example to move to the next linedit and the SLOT on editing finished triggers. You can also add an icon to the checkbox to show that something went wrong (for example the linedit doesnt contains what you want)

7348

hope it brings you some light!

Terreox
1st February 2012, 19:52
ok thanks for you solution.
it is a different way than i want but with yours i can show the user if something is wrong or not.
what i want is a bit different:
i put QRegEx on all boxes so that the user is limited while typing.
at the end of my dialog there is a "Next" button. If the user presses this button the program shall check if e.g. all boxes are filled with content. if only one box is empty than the user shall be noticed about that.
I tried the button_clicked() event and added an extra function in that event. this extra function shall check if every box is not empty anymore. but how can i tell the function what dialog he shall use? i need something that i can call e.g. lineedit->text() but how can i do that?



bool fieldChecker();

void Something::on_pushButton_clicked()
{
if(fieldChecker() != true){
QMessageBox::warning(this, "Error","lineedit is empty!");
}else{
QMessageBox::information(this, "Granted", "lineedit is not empty");
}
}

bool fieldChecker(){
if(lineedit->text() == ""){
return false;
}
return true;
}

The problem is: I cannot call lineedit->text() in fieldChecker(). What do i have to do to be able to use that?

KillGabio
1st February 2012, 19:58
the lineEdit is created by you or the Designer? if its created in Designed mode then you use it like this


this->ui->lineedit

Terreox
1st February 2012, 20:06
yeah that is what i tried too but it does not work.
if i try this in void Something::on_pushButton_clicked() it works.
but if i try this in bool fieldChecker() it does not.

KillGabio
1st February 2012, 20:13
mmmm try adding that function to the Something class...

for example:

Something.h


.
.
.
public:
bool fieldChecker ();
.
.
.


Something.cpp



bool Something::fieldChecker(){
if(ui->lineedit->text() == ""){
return false;
}
return true;
}


Still you havent told me if you create the linedit in Design or you create it :P

Terreox
2nd February 2012, 15:48
oh yes i forgot it sry.
i created the lineedit in the Designer.
Told with a friend about this problem and found the same solution.
Did not think about this way - the easy way :) The solution was too simple to think about it directly but i think it is normal as a newbie like me :)
Anyway many thanks to you!