Validating lineEdit fields
-
wrote on 13 Apr 2012, 01:55 last edited by
Hi,
I have a function where I'm getting the input from two lineEdit fields and then doing some calculations with those numbers. Everything is working fine (thanks to all of you that have helped me) but what I want is to check if the fields are empty or the input was a "0" (cero), I want to stop the program until a valid number is enter.
I tried a few things but nothing...
@void MainWindow::calculate()
{
//getting inputs as text
QString partW = ui->lineEdit_PartWidth->text();
QString partL = ui->lineEdit_PartLength->text();//converting input text as numbers partWidth = partW.toFloat(); partLength = partL.toFloat();
// here I tried with only one field but it doesn't work
if(partWidth = NULL)
{
ui->lineEdit_PartWidth->setText("0");
}// some calculations using the numbers from lineEdit fields
}@How can I check if the fields contain a valid number and are not empty?
Thanks a lot.
-
wrote on 13 Apr 2012, 02:55 last edited by
I would check the text property for empty and do the calculation accordingly before conversions as below:
@
//getting inputs as text
QString partW = ui->lineEdit_PartWidth->text();
QString partL = ui->lineEdit_PartLength->text();// change the if condition to check QString isEmpty() if (partW.isEmpty()) // or can be (ui->lineEdit_PartWidth->text().isEmpty()) { ui->lineEdit_PartWidth->setText("0"); }
@
Refer the manual for "bool QString::isEmpty () const":http://qt-project.org/doc/qt-4.8/qstring.html#isEmpty
Curious to know: why would you want to get the value from lineEdit_PartWidth and if it is empty set the same with "0"?
-
wrote on 13 Apr 2012, 06:58 last edited by
To validate the contents of a line edit, you should look into using a [[doc:QValidator]]. Its [[doc:QIntValidator]] subclass seems to be what you are looking for.
-
wrote on 13 Apr 2012, 11:54 last edited by
veeraps
This method worked thanks a lot.@if (partW.isEmpty())
{
ui->label_InfoScreen->setText("Please enter a valid number");
}@bq. To validate the contents of a line edit, you should look into using a QValidator. Its QIntValidator subclass seems to be what you are looking for.
My biggest problem is understanding the documentation, this may be because I 'm still learning C++. Can someone show me how to use the Qvalidator?
Thanks
-
wrote on 13 Apr 2012, 12:00 last edited by
Assuming the valid range is 1 - 100, you'd do:
@
ui->lineEdit->setValidator(new QIntValidator(1, 100, this));
@There is also a [[doc:QDoubleValidator]], if floating points is what you need instead.
-
wrote on 13 Apr 2012, 12:15 last edited by
@void MainWindow::calculate()
{
//getting inputs as text
QString partW = ui->lineEdit_PartWidth->text();
QString partL = ui->lineEdit_PartLength->text();//converting input text as numbers partWidth = partW.toFloat(); partLength = partL.toFloat();
ui->lineEdit_PartLength->setValidator(new QDoubleValidator(1, 100, this));
}// some calculations using the numbers from lineEdit fields
}@I tried the code above but I'm getting the following error.
bq. error: initializing argument 3 of 'QDoubleValidator::QDoubleValidator(double, double, int, QObject*)
Thanks a lot for your help
-
wrote on 13 Apr 2012, 12:17 last edited by
First of all, please just read the documentation for QDoubleValidator. The constructor for that class takes four values, not three... The documentation will tell you the meaning of all four.
Second, you need to set the validator somewhere else in your code. Probably in your MainWindow constructor.
-
wrote on 13 Apr 2012, 12:26 last edited by
Well, as I said my biggest frustration right now is that I don't know how to read the documentation, since there are no examples on how to use the classes and methods you are looking at, but as I said this may be because I don't fully understand C++.
I will give it another try.
Any tips on how to read the documentation? How to understand the usage of classes, methods, properties etc.
I know I know I'm a pain.
Thanks a lot
-
wrote on 13 Apr 2012, 12:32 last edited by
First step would be to get your C++ basics in order. Seriously, a basic understanding of the principle of C++ is needed to use Qt effectively. Then, follow some tutorials, or dig into some Qt books. There are several, and even some free ones available online. Learning to read the documentation is really important. The Qt documentation is generally considdered of excellent quality. For these kinds of issues, it should be enough. In this specific case, what is it you don't understand in the documentation of QDoubleValidator exactly?
-
wrote on 13 Apr 2012, 12:56 last edited by
I'm currently learning both C++ and Qt. I'm taking a C++ class and started to read two books about Qt .
As far as what I don't know understand about the QDoubleValidator function, well I'm afraid to say it but...
It starts listing the QDoubleValidator in to different ways, the first one I think its a pointer to the QObject class or something like that.
The second one takes four parameters but what double bottom, double top, int decimal and QObject *parent=0 are?
bq. QDoubleValidator ( QObject * parent = 0 )
QDoubleValidator ( double bottom, double top, int decimals, QObject * parent = 0 )What would be the smallest and simplest Class that I could start with?
-
wrote on 13 Apr 2012, 13:05 last edited by
The list of public functions contains two constructors (functions that you can use to create an instance of the class QDoubleIterator), one destructor (the one with the ~ in front) that is used to destroy an instance, and 9 normal public functions that you can call on an instance.
The constructor functions give the different parameters you can use when you create a new instance. In this case, there are options possible: either give 1, or give four arguments. However, because the last argument (the parent) is optional (it has default value 0, shown by the = 0 behind the argument), you might also give either 0 or 3 arguments. In my earlier example, I showed how to use this for a constructor taking 3 arguments (the last one being optional as well).
Quote from the documentation:
[quote]
QDoubleValidator::QDoubleValidator ( double bottom, double top, int decimals, QObject * parent = 0 )
Constructs a validator object with a parent object. This validator will accept doubles from bottom to top inclusive, with up to decimals digits after the decimal point.
[/quote] -
wrote on 13 Apr 2012, 13:07 last edited by
Both are constructors for the same class. In general it should not affect the size or complexity of the class itself.
You should decide on the basis of what you need. But have a look to the actual description there.
-
wrote on 13 Apr 2012, 13:46 last edited by
Ok, make sense, thank you for the good explanation.
Lets pretend that I do understand how to read the information in a class but I need to interact with a lineEdit to validate it which was my case, but I'm not familiar with lineEdit class.
This is how I would start finding inforamtion...
The first thing I would do is go to http://qt-project.org/doc/qt-4.8/classes.html find the lineEdit class and see what functions and propreties are avalable for lineEdit, unfortunately I would get stock here since I only know that I could use the function setValidator but how do I know that I need to use the QInt or QDoubleValidator?
What would be your procedure?
How do you start finding information for the validation of a lineEdit if you didnt know the class?
Sorry if I'm making this too long but I want to understand this better.
You guys are awesom, Thanks a lot.
-
wrote on 13 Apr 2012, 13:51 last edited by
Well, you'd see that you can set a QValidator (well, actually a QValidator*, but that's besides the point), so you go to the QValidator documentation. In there, you'll find a general description on how validators work, and you'll find that there three classes that inherit QValidator (and that QValidator itself inherits QObject). And because everwhere where you can use a QValidator, you can also use one of the classes that inherit QValidator, you go and read the documentation of these three classes to find out that QDoubleValidator suits your purpose best.
But, in order to be able to understand this way of using the docs, you need to understand what inheritance means in the context of C++, and for that, you need to go back to your C++ books and read the chapters on inheritance and polymorphism.
-
wrote on 13 Apr 2012, 14:20 last edited by
Ok I will try to digest all this information before I can ask more questions. As far as inheritance and polymorphism I do have a little understanding on how it works (little).
I will try to use the procedure you describe for my future needs, this doent mean I wont came back with more question though :(
Thanks a lot for your time.
-
wrote on 13 Apr 2012, 14:38 last edited by
bq. ... this doent mean I wont came back with more question though
By all means, feel free to come back if you get stuck! Everyone is willing to help, especially if it's obvious that you're doing your best to try to learn :) Good luck!
-
wrote on 13 Apr 2012, 14:54 last edited by
You guys are awesome! Thanks a lot
1/17