Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7613667
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T02:12:37+00:00 2026-05-31T02:12:37+00:00

I want to create a doublespin box that changes values in steps of 0.2.

  • 0

I want to create a doublespin box that changes values in steps of 0.2. But when the user enters a value that is not correct according to the steps. I normalizes that to the nearest correct value.

I tried something like the code shown below but I don’t know how to stop values like 0.5 to be entered. Please help me on this.


    from PyQt4.QtCore import *
    from PyQt4.QtGui import *

    class SigSlot(QWidget):
        def __init__(self, parent=None):
            QWidget.__init__(self, parent)
            self.setWindowTitle('spinbox value')
            self.resize(250,150)
            self.lcd1 = QLCDNumber(self)
            self.spinbox1 = QDoubleSpinBox(self)
            self.spinbox1.setSingleStep(0.2)
            self.spinbox1.setCorrectionMode(1)
             # create a Grid Layout
            grid = QGridLayout()
            grid.addWidget(self.lcd1, 0, 0)
            grid.addWidget(self.spinbox1, 1, 0)
            self.setLayout(grid)
             # allows access to the spinbox value as it changes
            self.connect(self.spinbox1, SIGNAL('valueChanged(double)'), self.change_value1)

        def change_value1(self, event):
            val = self.spinbox1.value()
            self.lcd1.display(val)

    app = QApplication([])
    qb = SigSlot()
    qb.show()
    app.exec_()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T02:12:38+00:00Added an answer on May 31, 2026 at 2:12 am

    You have two choices:

    • You can subclass the QSpinBox, override validate method and use an appropriate Q*Validator (e.g. QRegExpValidator) inside.
    • You can check the value in slot connected to valueChanged before using and correct it if necessary.

    Since you are already using the valueChanged signal, second option should be fairly easy to implement. Just change your change_value method like this:

    def change_value1(self, val): # new value is passed as an argument
        # so no need for this
        # val = self.spinbox1.value()
    
        new_val = round(val*5)/5 # one way to fix
        if val != new_val:       # if value is changed, put it in the spinbox
            self.spinbox1.setValue(new_val)
    
        self.lcd1.display(new_val)
    

    By the way, since you are using only one decimal precision, it might be logical to also use:

    self.spinbox1.setDecimals(1)
    

    in your __init__. And try to use the new style signals and slots. i.e.:

    self.connect(self.spinbox1, SIGNAL('valueChanged(double)'), self.change_value1)
    

    could be written as:

    self.spinbox1.valueChanged[float].connect(self.change_value1)
    

    Edit

    Subclassing:

    class MySpinBox(QDoubleSpinBox):
        def __init__(self, parent=None):
            super(MySpinBox, self).__init__(parent)
            # any RegExp that matches the allowed input
            self.validator = QRegExpValidator(QRegExp("\\d+[\\.]{0,1}[02468]{0,1}"), self)
    
        def validate(self, text, pos):
            # this decides if the entered value should be accepted
            return self.validator.validate(text, pos)
    

    then instead of using QDoubleSpinBox you would use MySpinBox and leave the input checking to this class.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want create module which update list of usb devices automatically (not only mass
I am trying ebook reading app. In that I want create UIActionSheet by clicking
I want to create a textbox which text binds an integer (e.g 123456789), but
I want to create a bookshelf that tiles horizontally and vertically. I have three
I want create a tarball that when extracted the file(s) will be placed at
I want create texture atlas(matrix of images) from multiple images.Is their any applescript that
I want create a cache so that when an item doesn't exist, only one
I want create a function that verify if the numbers of a list fall
I want create something that looks like the MSN Messenger chat forms. I am
I want create a project where every user can have his own 'homepage'. So

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.