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. Setting dot as a seperator in LineEdit
QtWS25 Last Chance

Setting dot as a seperator in LineEdit

Scheduled Pinned Locked Moved Unsolved General and Desktop
15 Posts 5 Posters 1.3k 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.
  • D Offline
    D Offline
    deleted286
    wrote on last edited by
    #1

    I have a line edit. Im using QDoubleValidator for accepting only double values. I want to seperate my double numbers using dot.
    Because i have a json file, when i use comma it gives me wrong results.

    How can i set this?

    jsulmJ JonBJ 2 Replies Last reply
    0
    • Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Again: Use a QDoubleSpinBox !

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      D 1 Reply Last reply
      3
      • D deleted286

        I have a line edit. Im using QDoubleValidator for accepting only double values. I want to seperate my double numbers using dot.
        Because i have a json file, when i use comma it gives me wrong results.

        How can i set this?

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #3

        @suslucoder said in Setting dot as a seperator in LineEdit:

        Because i have a json file, when i use comma it gives me wrong results.

        Then do it properly when storing to JSON instead of forcing user to use dot!

        double d = lineEdit->text().toDouble();
        

        And store that d in JSON.

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        D 1 Reply Last reply
        3
        • Christian EhrlicherC Christian Ehrlicher

          Again: Use a QDoubleSpinBox !

          D Offline
          D Offline
          deleted286
          wrote on last edited by
          #4

          @Christian-Ehrlicher
          But, Im using line edit and i will search a solution for it.

          Christian EhrlicherC 1 Reply Last reply
          -1
          • jsulmJ jsulm

            @suslucoder said in Setting dot as a seperator in LineEdit:

            Because i have a json file, when i use comma it gives me wrong results.

            Then do it properly when storing to JSON instead of forcing user to use dot!

            double d = lineEdit->text().toDouble();
            

            And store that d in JSON.

            D Offline
            D Offline
            deleted286
            wrote on last edited by
            #5

            @jsulm thank you

            1 Reply Last reply
            0
            • D deleted286

              @Christian-Ehrlicher
              But, Im using line edit and i will search a solution for it.

              Christian EhrlicherC Online
              Christian EhrlicherC Online
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @suslucoder said in Setting dot as a seperator in LineEdit:

              But, Im using line edit and i will search a solution for it.

              I really don't understand why you're trying to reinvent the wheel. Esp. when there is so few knowledge ... :/

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              1 Reply Last reply
              0
              • D deleted286

                I have a line edit. Im using QDoubleValidator for accepting only double values. I want to seperate my double numbers using dot.
                Because i have a json file, when i use comma it gives me wrong results.

                How can i set this?

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by
                #7

                @suslucoder
                Best is to use a QDoubleSpinbox, then there are no conversion issues.

                However, if you are determined to use a QLineEdit. Do not just copy the string out of that to the JSON file. As @jsulm shows you should convert it first to a C++ double, and that is what should be saved to JSON. So if the user types a comma you may or may not convert that to a double given your language locale, as you please, but it's not what will end of in the file when Qt saves a double to JSON.

                Christian EhrlicherC 1 Reply Last reply
                2
                • JonBJ JonB

                  @suslucoder
                  Best is to use a QDoubleSpinbox, then there are no conversion issues.

                  However, if you are determined to use a QLineEdit. Do not just copy the string out of that to the JSON file. As @jsulm shows you should convert it first to a C++ double, and that is what should be saved to JSON. So if the user types a comma you may or may not convert that to a double given your language locale, as you please, but it's not what will end of in the file when Qt saves a double to JSON.

                  Christian EhrlicherC Online
                  Christian EhrlicherC Online
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on last edited by Christian Ehrlicher
                  #8

                  @JonB There is no difference between a QDoubleSpinBox and a QLineEdit except you have to not fiddle around with a regexp and you get a proper double value instead a QString.

                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                  Visit the Qt Academy at https://academy.qt.io/catalog

                  JonBJ 1 Reply Last reply
                  1
                  • Christian EhrlicherC Christian Ehrlicher

                    @JonB There is no difference between a QDoubleSpinBox and a QLineEdit except you have to not fiddle around with a regexp and you get a proper double value instead a QString.

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by JonB
                    #9

                    @Christian-Ehrlicher
                    ? Yes there is: QDoubleSpinbox has a double value() const, which QLineEdit does not, and I think that might be very useful for the OP!
                    Ah yes, you've noted that, so I don't follow what your point is.

                    If you mean: should he use a QDoubleSpinbox instead, then yes, in another thread elsewhere that the OP asked about this I was the first to say he should use a spin box! And I said that again here. I just tried to answer given that he states he wants to use a QLineEdit instead.

                    D 1 Reply Last reply
                    1
                    • JonBJ JonB

                      @Christian-Ehrlicher
                      ? Yes there is: QDoubleSpinbox has a double value() const, which QLineEdit does not, and I think that might be very useful for the OP!
                      Ah yes, you've noted that, so I don't follow what your point is.

                      If you mean: should he use a QDoubleSpinbox instead, then yes, in another thread elsewhere that the OP asked about this I was the first to say he should use a spin box! And I said that again here. I just tried to answer given that he states he wants to use a QLineEdit instead.

                      D Offline
                      D Offline
                      deleted286
                      wrote on last edited by
                      #10

                      @JonB Thank you for your answer. I know using double spin box may be more efficient. But i started to do it with line edit and i wanted to know how can i solve this problem when i use it.

                      Now, im trying to do it with double spin box as most people suggest to me. But it seperate numbers with comma, can i use dot instead of comma? Is there any property for it?

                      JonBJ 1 Reply Last reply
                      0
                      • D deleted286

                        @JonB Thank you for your answer. I know using double spin box may be more efficient. But i started to do it with line edit and i wanted to know how can i solve this problem when i use it.

                        Now, im trying to do it with double spin box as most people suggest to me. But it seperate numbers with comma, can i use dot instead of comma? Is there any property for it?

                        JonBJ Offline
                        JonBJ Offline
                        JonB
                        wrote on last edited by JonB
                        #11

                        @suslucoder
                        We have tried to explain to you that you are looking at the wrong thing. It doesn't matter what you type/how it displays in a QLineEdit. So long as you do not just store/pass the QLineEdit::text() to the JSON. That is the wrong thing to do for a numeric. You need to get the double value of whatever the user types there --- you can allow dots or commas as you please, depending on what code you call to convert it to a number --- and pass that double value. then JSON will see a numeric and output it with a dot, regardless of your locale, which is what is needed in JSON.

                        Can't say this any more times.

                        D 1 Reply Last reply
                        0
                        • JonBJ JonB

                          @suslucoder
                          We have tried to explain to you that you are looking at the wrong thing. It doesn't matter what you type/how it displays in a QLineEdit. So long as you do not just store/pass the QLineEdit::text() to the JSON. That is the wrong thing to do for a numeric. You need to get the double value of whatever the user types there --- you can allow dots or commas as you please, depending on what code you call to convert it to a number --- and pass that double value. then JSON will see a numeric and output it with a dot, regardless of your locale, which is what is needed in JSON.

                          Can't say this any more times.

                          D Offline
                          D Offline
                          deleted286
                          wrote on last edited by deleted286
                          #12

                          @JonB I understood what you are saying. I have double spin box on my ui. It seperate with comma. Like 5,000. But i want to seperate it dot. Or it should accept comma and dot both.
                          And i cannot gives a number like 630,12. Or i want to give 2500 but it does not allow.

                          JonBJ 1 Reply Last reply
                          0
                          • D deleted286

                            @JonB I understood what you are saying. I have double spin box on my ui. It seperate with comma. Like 5,000. But i want to seperate it dot. Or it should accept comma and dot both.
                            And i cannot gives a number like 630,12. Or i want to give 2500 but it does not allow.

                            JonBJ Offline
                            JonBJ Offline
                            JonB
                            wrote on last edited by JonB
                            #13

                            @suslucoder
                            I'm sorry but I (and others) have tried to explain what you need and what to do so many times, I cannot find another way of putting it.

                            1 Reply Last reply
                            0
                            • Christian EhrlicherC Online
                              Christian EhrlicherC Online
                              Christian Ehrlicher
                              Lifetime Qt Champion
                              wrote on last edited by
                              #14

                              If you can enter a point or comma depends on your locale. If you need another locale for your QDoubleSpinBox (which is very confusing for the user) use QWidget::setLocale()

                              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                              Visit the Qt Academy at https://academy.qt.io/catalog

                              1 Reply Last reply
                              3
                              • S Offline
                                S Offline
                                SimonSchroeder
                                wrote on last edited by
                                #15

                                Just as @Christian-Ehrlicher said you need to set the proper locale to use a dot instead of a comma. However, there is no built-in way to allow for both comman and dot at the same time. You can try to have your own class derived from QDoubleValidator which replaces commas with a point in validate().

                                I have to disagree that QDoubleSpinbox is superior to QLineEdit. I work on a scientific software. For historic reasons double input is rarely a spin box. But, it is not so easy to replace it with a spin box. If both 1000 as well as 1e-5 is a valid input there is no specific step size you can set for a spin box that makes sense. Most of the time a line edit with a validator (we also want to support both comma and point) is sufficient for the job. Yes, you do have to convert the text to a double explicitly.

                                If you use the same thing repeatedly you should write your own little class deriving from either QLineEdit or QDoubleSpinbox. For the line edit you could then implement double value() const method. And if there is no step size for your spin box that makes sense, then IMHO it makes more sense to derive from QLineEdit.

                                1 Reply Last reply
                                0

                                • Login

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