Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Validating lineEdit fields
Forum Update on Monday, May 27th 2025

Validating lineEdit fields

Scheduled Pinned Locked Moved General and Desktop
17 Posts 5 Posters 15.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    fs_tigre
    wrote on 13 Apr 2012, 01:55 last edited by
    #1

    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.

    1 Reply Last reply
    0
    • V Offline
      V Offline
      veeraps
      wrote on 13 Apr 2012, 02:55 last edited by
      #2

      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"?

      1 Reply Last reply
      0
      • A Offline
        A Offline
        andre
        wrote on 13 Apr 2012, 06:58 last edited by
        #3

        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.

        1 Reply Last reply
        0
        • F Offline
          F Offline
          fs_tigre
          wrote on 13 Apr 2012, 11:54 last edited by
          #4

          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

          1 Reply Last reply
          0
          • A Offline
            A Offline
            andre
            wrote on 13 Apr 2012, 12:00 last edited by
            #5

            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.

            1 Reply Last reply
            0
            • F Offline
              F Offline
              fs_tigre
              wrote on 13 Apr 2012, 12:15 last edited by
              #6

              @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

              1 Reply Last reply
              0
              • A Offline
                A Offline
                andre
                wrote on 13 Apr 2012, 12:17 last edited by
                #7

                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.

                1 Reply Last reply
                0
                • F Offline
                  F Offline
                  fs_tigre
                  wrote on 13 Apr 2012, 12:26 last edited by
                  #8

                  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

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    andre
                    wrote on 13 Apr 2012, 12:32 last edited by
                    #9

                    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?

                    1 Reply Last reply
                    0
                    • F Offline
                      F Offline
                      fs_tigre
                      wrote on 13 Apr 2012, 12:56 last edited by
                      #10

                      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?

                      1 Reply Last reply
                      0
                      • A Offline
                        A Offline
                        andre
                        wrote on 13 Apr 2012, 13:05 last edited by
                        #11

                        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]

                        1 Reply Last reply
                        0
                        • K Offline
                          K Offline
                          koahnig
                          wrote on 13 Apr 2012, 13:07 last edited by
                          #12

                          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.

                          Vote the answer(s) that helped you to solve your issue(s)

                          1 Reply Last reply
                          0
                          • F Offline
                            F Offline
                            fs_tigre
                            wrote on 13 Apr 2012, 13:46 last edited by
                            #13

                            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.

                            1 Reply Last reply
                            0
                            • A Offline
                              A Offline
                              andre
                              wrote on 13 Apr 2012, 13:51 last edited by
                              #14

                              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.

                              1 Reply Last reply
                              0
                              • F Offline
                                F Offline
                                fs_tigre
                                wrote on 13 Apr 2012, 14:20 last edited by
                                #15

                                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.

                                1 Reply Last reply
                                0
                                • M Offline
                                  M Offline
                                  mlong
                                  wrote on 13 Apr 2012, 14:38 last edited by
                                  #16

                                  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!

                                  Software Engineer
                                  My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

                                  1 Reply Last reply
                                  0
                                  • F Offline
                                    F Offline
                                    fs_tigre
                                    wrote on 13 Apr 2012, 14:54 last edited by
                                    #17

                                    You guys are awesome! Thanks a lot

                                    1 Reply Last reply
                                    0

                                    1/17

                                    13 Apr 2012, 01:55

                                    • Login

                                    • Login or register to search.
                                    1 out of 17
                                    • First post
                                      1/17
                                      Last post
                                    0
                                    • Categories
                                    • Recent
                                    • Tags
                                    • Popular
                                    • Users
                                    • Groups
                                    • Search
                                    • Get Qt Extensions
                                    • Unsolved