April 4, 2011

mchris357 mchris357
Lab Rat
23 posts

How to enable a submit button after multiple form fields have been verified.

 

Hello again,

This post is an unrelated followup to this post:

http://developer.qt.nokia.com/forums/viewthread/4850

If I have a form with 4 line edits and a disabled ok button that I would like to enable once the lineedits are all validated, how do I check to verify all of the lineedits are validated? Do I need to have a variable in the form class that indicates the number of validated fields and check that every time I receive an editingFinished() signal? (If they’re all validated, then it would have a value of 4 and the ok button would enable). Is there a better way?

Thanks!!

12 replies

April 4, 2011

Volker Volker
Robot Herder
5428 posts

You should connect signal textChanged() [doc.qt.nokia.com] of all four line edits to a single slot. In that slot, check if all four line edits are filled with some text and set the ok button enabled on the result of your calcualtions. It would look something like this:

  1. YourDialog::YourDialog(QWidget *parent)
  2. {
  3.     connect(ui->lineEdit1, SIGNAL(textChanged()), this, SLOT(checkLineEdits()));
  4.     connect(ui->lineEdit2, SIGNAL(textChanged()), this, SLOT(checkLineEdits()));
  5.     connect(ui->lineEdit3, SIGNAL(textChanged()), this, SLOT(checkLineEdits()));
  6.     connect(ui->lineEdit4, SIGNAL(textChanged()), this, SLOT(checkLineEdits()));
  7. }
  8.  
  9.  
  10. void YourDialog::checkLineEdits()
  11. {
  12.     bool ok = !ui->lineEdit1->text().isEmpty()
  13.         && !ui->lineEdit2->text().isEmpty()
  14.         && !ui->lineEdit3->text().isEmpty()
  15.         && !ui->lineEdit4->text().isEmpty();
  16.  
  17.     ui->okButton->setEnabled(ok);
  18. }

April 4, 2011

ZapB ZapB
Robot Herder
1359 posts

Yes, do as Volker suggests. This is a common pattern that you will likely use quite often.

 Signature 

Nokia Certified Qt Specialist
Interested in hearing about Qt related work

April 5, 2011

VCsala VCsala
Lab Rat
339 posts

The method Volker suggested is the safe way to do it and as I understand it uses some kind of state machine for storing the actual state of validity therefore it does not cause significant overhead even if your validation is resource hungry.

April 5, 2011

Andre Andre
Area 51 Engineer
6076 posts

I have seen this same question not too long ago on this forum. Not sure it was the same one asking though.

While Volker’s code is absolutely right, personally, I find something like this more readable:

  1. void YourDialog::checkLineEdits()
  2. {
  3.     bool ok(true);
  4.     ok &= !ui->lineEdit1->text().isEmpty();
  5.     ok &= !ui->lineEdit2->text().isEmpty();
  6.     ok &= !ui->lineEdit3->text().isEmpty();
  7.     ok &= !ui->lineEdit4->text().isEmpty();
  8.  
  9.     ui->okButton->setEnabled(ok);
  10. }

And I know people here are going to disagree with me. :-)
Of course, variations are possible too, for instance one where you iterate over all QLineEdits (no matter how many there are):

  1.  
  2. void YourDialog::checkLineEdits()
  3. {
  4. bool ok(true);
  5. QList<QLineEdit*> lineEditList = findChildren<QLineEdit*>();
  6. foreach (QLineEdit* le, lineEditList)
  7.     ok &= !le->text().isEmpty();
  8.  
  9.     ui->okButton->setEnabled(ok);
  10. }

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

April 5, 2011

peppe peppe
Ant Farmer
1026 posts

It makes little sense to use bitwise operators on a bool, doesn’t it? :P

 Signature 

Software Engineer
KDAB (UK) Ltd., a KDAB Group company

April 5, 2011

Johan Solo Johan Solo
Lab Rat
94 posts
peppe wrote:
It makes little sense to use bitwise operators on a bool, doesn’t it? :P

How would you do then? On linux you can use ‘*=’ instead of ‘&=’ if both operands are bool, but I was told by an expert that (at least some) windows compiler just cannot stand such a notation.

 Signature 

Linux : you can find worse, but it’s more expensive.

April 5, 2011

peppe peppe
Ant Farmer
1026 posts

I was just punning on:

And I know people here are going to disagree with me. :-)

In C unfortunately there’s no short-circuiting operator &&= (which would be required in this case).

 Signature 

Software Engineer
KDAB (UK) Ltd., a KDAB Group company

April 5, 2011

Andre Andre
Area 51 Engineer
6076 posts
peppe wrote:
It makes little sense to use bitwise operators on a bool, doesn’t it? :P

Ah, well, they just are very short. Just one bit to compare bitwise. It works nonetheless.

If you want to do it “right”, I guess you’d have to do this:

  1. void YourDialog::checkLineEdits()
  2. {
  3.     bool ok(true);
  4.     ok = ok && !ui->lineEdit1->text().isEmpty();
  5.     ok = ok && !ui->lineEdit2->text().isEmpty();
  6.     ok = ok && !ui->lineEdit3->text().isEmpty();
  7.     ok = ok && !ui->lineEdit4->text().isEmpty();
  8.  
  9.     ui->okButton->setEnabled(ok);
  10. }

Does that get more readable? Not really, I think.

P.S. I was expecting comments on this not being efficient, actually. :-)

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

April 5, 2011

Volker Volker
Robot Herder
5428 posts
VCsala wrote:
The method Volker suggested is the safe way to do it and as I understand it uses some kind of state machine for storing the actual state of validity therefore it does not cause significant overhead even if your validation is resource hungry.

There is no state machine involved. The code always checks all four line edits. There may be some short circuiting by the compiler, if the first check is false, the complete expression is false, so there is no need to calculate the rest.

April 5, 2011

VCsala VCsala
Lab Rat
339 posts
Volker wrote:
VCsala wrote:
The method Volker suggested is the safe way to do it and as I understand it uses some kind of state machine for storing the actual state of validity therefore it does not cause significant overhead even if your validation is resource hungry.

There is no state machine involved. The code always checks all four line edits. There may be some short circuiting by the compiler, if the first check is false, the complete expression is false, so there is no need to calculate the rest.

However, if you want to have more complex check on the input field (and not only checking if it is empty or not) and you use validator (which is a more generic solution for this use case) then you have validity states (and in this case the validation itself can be resource and time consuming). As I know even in this case the above pattern is safe as the states are recalculated only if the field has been changed.

April 6, 2011

mchris357 mchris357
Lab Rat
23 posts

Thanks for all the input! QT certainly has an active AND helpful community…something that can be difficult to find in combination.

April 6, 2011

ZapB ZapB
Robot Herder
1359 posts

Don’t forget good looking too ;-)

Good luck with your project!

 Signature 

Nokia Certified Qt Specialist
Interested in hearing about Qt related work

 
  ‹‹ TTL on UDP sockets      [Solved] QTableView + QAbstractItemDelegate: Custom editor doesnt close on enter or when "clicking away"? ››

You must log in to post a reply. Not a member yet? Register here!