Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. How to restrict QSpinBox to take more the one Zero ??
Forum Update on Monday, May 27th 2025

How to restrict QSpinBox to take more the one Zero ??

Scheduled Pinned Locked Moved Unsolved Mobile and Embedded
4 Posts 3 Posters 904 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.
  • K Offline
    K Offline
    kishore_hemmady
    wrote on last edited by
    #1

    Hi,

    I have this slot in mainwindow.cpp
    void MainWindow::on_blend_valueChanged(int arg1)
    {
    }
    in the UI in the blend field,
    A field can allow upto max only one zero and a non zero number cannot be prefixed with a 0. For example : 0,1,2 is allowed ,but not 00,01,002..

    how can i do this,please help me.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      From the top of my head:

      • subclass QSpinBox
      • Create a QLineEdit
      • Create a QRegularExpressionValidator with the corresponding expression
      • Set the validator on the line dit
      • Set the line edit on the spin box

      Out of curiosity, why do you want to limit the number of zeroes ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • K Offline
        K Offline
        kishore_hemmady
        wrote on last edited by kishore_hemmady
        #3

        thank you @SGaist
        I am also thinking the same,but its a useless requirement.
        Have to do.

        1 Reply Last reply
        0
        • Gojir4G Offline
          Gojir4G Offline
          Gojir4
          wrote on last edited by
          #4

          Hello,

          I may have misunderstood, but if you want value to be always the same size and prefixed with 0, you can subclass QSpinBox and reimplement textFromValue():

          class CustomSpinBox : public QSpinBox
          {
              Q_OBJECT
          public:
              explicit CustomSpinBox(QWidget *parent = nullptr) : QSpinBox(parent)
              {}
          
          protected:
              virtual QString textFromValue(int val) const
              {
                  //Ensure "width" of the field is always 4
                  //base 10 (decimal)
                  //fill with '0' character
                  return QString("%1").arg(val, 4, 10, QChar('0'));
              }
          };
          
          1 Reply Last reply
          2

          • Login

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