Uploaded image for project: 'Qt for Python'
  1. Qt for Python
  2. PYSIDE-3014

fixup in QSpinBox does not work

    XMLWordPrintable

Details

    • Bug
    • Resolution: Fixed
    • P3: Somewhat important
    • 6.9.0, 6.8.3
    • 6.8.1
    • PySide
    • None
    • Linux/X11
    • af7a0c68c (dev), 83445c3ae (6.8)

    Description

      QSpinBox (QAbstractSpinBox) allows custom validation by re-implementing validate and fixup.

      fixup signature is void fixup(QString &) and is supposed to alter the input passed by reference. In PySide, the signature is adjusted so that the function returns a string. However, this does not seem to work. The returned value is ignored.

      See the example below. It shows a QSpinBox that would allow only multiples of 5. Type a number that is not a multiple of five, and unfocus the window.

      Expected behavior: the value is rounded to the next 5, e.g. typing in 6 should change the text to 10.

      Observed behavior: the previous value in the spinbox (for example 0) is restored no matter what the input was.

      from PySide6.QtGui import QValidator
      from PySide6.QtWidgets import QApplication, QSpinBox
      
      class MySpinBox(QSpinBox):
          def __init__(self, parent=None):
              super().__init__(parent)
      
          def stepBy(self, steps):
              self.setValue(self.value() + 5 * steps)
      
          def validate(self, input, pos):
              if not input:
                  return QValidator.State.Intermediate
      
              try:
                  number = int(input)
              except ValueError:
                  return QValidator.State.Invalid
      
              if number % 5 == 0:
                  return QValidator.State.Acceptable
              else:
                  return QValidator.State.Intermediate
      
          def fixup(self, input):
              number = int(input)
              # Round up to next multiple of 5.
              number += 5 - number % 5
              return str(number)
      
      if __name__ == "__main__":
          app = QApplication()
          spinbox = MySpinBox()
          spinbox.show()
          app.exec()
      

      See also a similar previous issue affecting QValidator https://bugreports.qt.io/browse/PYSIDE-106

      Attachments

        For Gerrit Dashboard: PYSIDE-3014
        # Subject Branch Project Status CR V

        Activity

          People

            kleint Friedemann Kleint
            basyskom-broulik Kai Uwe Broulik
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes