PyQt5 control QSpinBox

PyQt5 control QSpinBox


1. QSpinBox

QSPINBox is a counter control that allows users to select an integer value by clicking up or down or pressing the up and down keys on the keyboard to increase and decrease the currently displayed value. Of course, the user can also enter the value
. (0-99), the step size of each change is 1
QSpinBox class and QDoubleSpinbox class are derived from QAbstractSpinBox class, QSpinBox is used to handle integer values, QDoubleSpinBox is used to handle floating-point values, the difference between them is to process data Different types, the other functions are basically the same. The default precision of QDoubleSpinBox is two decimal places, but it can be changed by setDecimals ()

1.1. Common methods in the QSpinBox class

  • init(parent: QWidget = None)
  • cleanText() → str
  • displayIntegerBase() → int
  • event(QEvent) → bool
  • fixup(str) → str
  • maximum() → int
  • minimum() → int
  • prefix () → str # Get the prefix
  • setDisplayIntegerBase (int) # Hex setting
  • setMaximum (int) # maximum value
  • setMinimum(int)
  • setPrefix (str) # set prefix
  • setRange (int, int) # Set the range
  • setSingleStep (int) # Set step size
  • setStepType (StepType) # unknown
  • setSuffix (str) # Set prefix-
  • setValue(int)
  • singleStep () → int # get step value
  • stepType() → StepType
  • suffix () → str # get suffix
  • textFromValue(int) → str
  • validate(str, int) → (State, str, int)
  • value() → int
  • valueFromText(str) → int

1.2. Signals

  • textChanged(str)
  • valueChanged(int)
  • valueChanged(str)

2. Experimental code

  
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *


class MainWidget(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)

        self.resize(300, 100)

        # 垂直布局
        layout = QVBoxLayout()

        # 标签
        self.l1 = QLabel("current value:")
        self.l1.setAlignment(Qt.AlignCenter)

        layout.addWidget(self.l1)

        # 计数器
        self.spinBox = QSpinBox()
        self.spinBox.setRange(0, 300)
        self.spinBox.setSingleStep(5)
        layout.addWidget(self.spinBox)

        self.spinBox.valueChanged.connect(self.onValueChanged)

        self.setLayout(layout)

    def onValueChanged(self, value):
        #print("current value is {0}".format(value))
        self.l1.setText("current value: {}".format(self.spinBox.value()))
        
if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MainWidget()
    window.resize(400, 200)
    window.show()

    sys.exit(app.exec_())

Guess you like

Origin www.cnblogs.com/wodeboke-y/p/12697202.html