1

I am writing an application with a submit button but I want to check if the textfield has been filled if not do not submit.

Not quite sure how the code should go.

My code currently looks like

if(flightNumber == NULL)
{
   flightNumbertext.text.color = 'red';
}

I am designing this for a Qt Quick application written in QML.

NG_
  • 6,895
  • 7
  • 45
  • 67
Ismael
  • 343
  • 1
  • 4
  • 7
  • Check the docs for the widget you're using for `flightNumber`. If it's a QLineEdit, for example, check its [text()](http://doc.qt.nokia.com/latest/qlineedit.html#text-prop) property. – vanza Jun 30 '11 at 07:19
  • hi please is there anyway I can check the input in an email field fulfills the conditions for being a valid email address. – Ismael Jun 30 '11 at 07:24
  • See http://stackoverflow.com/questions/1778390/check-if-email-are-valid-and-exists – MSalters Jun 30 '11 at 07:59
  • I managed to edit the code and the following does work (flightNumber.text.length == 0) but leaves a loop hole in that it allows white spaces to be considered as values. Trying to use the isEmpty or iNull so far results in an error telling me flightNumber isn't a function which I kind of agree with but just can't figure out my way around this since using QTQuick – Ismael Jun 30 '11 at 08:44

2 Answers2

4

QT's editing widgets use QString.

QString has many methods that you can use.

Use isEmpty or isNull methods of QString.

I hope this helps.

O.C.
  • 6,711
  • 1
  • 25
  • 26
  • I managed to edit the code and the following does work (flightNumber.text.length == 0) but leaves a loop hole in that it allows white spaces to be considered as values. Trying to use the isEmpty or iNull so far results in an error telling me flightNumber isn't a function which I kind of agree with but just can't figure out my way around this since using QTQuick – Ismael Jun 30 '11 at 08:44
0

After a bit of playing, I think this will work for you

if (flightNumber.text.trim().length == 0) {
    flightNumbertext.text.color = 'red'; 
}

The function trim() is like QString::trimmed(). It removes both leading and trailing spaces from the string.

jwernerny
  • 6,978
  • 2
  • 31
  • 32