May 2019
Intermediate to advanced
542 pages
13h 37m
English
As you learned earlier under the Creating basic QtWidgets widgets section, QSpinBox can be used for discrete lists of string values, much like a combobox. QSpinBox has a built-in validate() method that works just like the QValidator class' method to constrain input to the widget. To make a spinbox use discrete string lists, we need to subclass QSpinBox and override validate() and two other methods, valueFromText() and textFromValue().
Let's create a custom spinbox class that can be used to choose items from a list; just before the MainWindow class, enter this:
class ChoiceSpinBox(qtw.QSpinBox): """A spinbox for selecting choices.""" def __init__(self, choices, *args, **kwargs): self.choices = choices super().__init__( ...
Read now
Unlock full access