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. QDoubleSpinBox->setValue() truncates floats/doubles
Forum Updated to NodeBB v4.3 + New Features

QDoubleSpinBox->setValue() truncates floats/doubles

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 4 Posters 932 Views 2 Watching
  • 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.
  • richferraraR Offline
    richferraraR Offline
    richferrara
    wrote on last edited by richferrara
    #1

    When I call setValue() on a QDoubleSpinBox, the value is truncated to an integer unless the difference between the current value and the new value is less than 1.

    Example:
    The "decimals" property of the spin box is set to 4.
    spin box current value = 50
    1st call to setValue(98.42) --> value changes to 98
    2nd call to setValue(98.42) --> value changes to 98.42

    Christian EhrlicherC 1 Reply Last reply
    0
    • richferraraR richferrara

      When I call setValue() on a QDoubleSpinBox, the value is truncated to an integer unless the difference between the current value and the new value is less than 1.

      Example:
      The "decimals" property of the spin box is set to 4.
      spin box current value = 50
      1st call to setValue(98.42) --> value changes to 98
      2nd call to setValue(98.42) --> value changes to 98.42

      Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by Christian Ehrlicher
      #2

      Works fine for me with Qt6.7:

      #include <QtWidgets>
      
      int main(int argc, char* argv[])
      {
          QApplication app(argc, argv);
          QDoubleSpinBox dbl;
          dbl.setDecimals(4);
          dbl.show();
          dbl.setValue(1.12);
          qDebug() << dbl.value();
          QTimer::singleShot(1000, [&] {
              dbl.setValue(98.42);
              qDebug() << dbl.value();
              });
          QTimer::singleShot(2000, [&] {
              dbl.setValue(98.42);
              qDebug() << dbl.value();
              });
          
          return app.exec();
      }
      

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

      richferraraR 1 Reply Last reply
      1
      • Christian EhrlicherC Christian Ehrlicher

        Works fine for me with Qt6.7:

        #include <QtWidgets>
        
        int main(int argc, char* argv[])
        {
            QApplication app(argc, argv);
            QDoubleSpinBox dbl;
            dbl.setDecimals(4);
            dbl.show();
            dbl.setValue(1.12);
            qDebug() << dbl.value();
            QTimer::singleShot(1000, [&] {
                dbl.setValue(98.42);
                qDebug() << dbl.value();
                });
            QTimer::singleShot(2000, [&] {
                dbl.setValue(98.42);
                qDebug() << dbl.value();
                });
            
            return app.exec();
        }
        
        richferraraR Offline
        richferraraR Offline
        richferrara
        wrote on last edited by
        #3

        @Christian-Ehrlicher It works okay for me with Qt 6.6 but not with Qt 5.15. It must have been fixed sometime in between.

        M C 2 Replies Last reply
        0
        • richferraraR richferrara

          @Christian-Ehrlicher It works okay for me with Qt 6.6 but not with Qt 5.15. It must have been fixed sometime in between.

          M Offline
          M Offline
          mpergand
          wrote on last edited by
          #4

          @Christian Ehrlicher's code works as expected for me with Qt 5.15.2

          1 Reply Last reply
          2
          • richferraraR richferrara

            @Christian-Ehrlicher It works okay for me with Qt 6.6 but not with Qt 5.15. It must have been fixed sometime in between.

            C Offline
            C Offline
            ChrisW67
            wrote on last edited by
            #5

            @richferrara ... and for me on 5.15.3 on Linux

            richferraraR 1 Reply Last reply
            1
            • C ChrisW67

              @richferrara ... and for me on 5.15.3 on Linux

              richferraraR Offline
              richferraraR Offline
              richferrara
              wrote on last edited by
              #6

              @ChrisW67 I'm using 5.15.2 on Windows 10.

              Christian EhrlicherC 1 Reply Last reply
              0
              • richferraraR richferrara

                @ChrisW67 I'm using 5.15.2 on Windows 10.

                Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @richferrara So did you try my testcase? What was printed on the console?

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

                richferraraR 1 Reply Last reply
                0
                • Christian EhrlicherC Christian Ehrlicher

                  @richferrara So did you try my testcase? What was printed on the console?

                  richferraraR Offline
                  richferraraR Offline
                  richferrara
                  wrote on last edited by
                  #8

                  @Christian-Ehrlicher I get the correct values (1.12, 98.42, 98.42). I don't understand why it's different in my Qt Creator application.

                  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