class Demo(QWidget): def __init__(self): super(Demo, self).__init__() # setWindowTitle()方法可以设置窗口标题; self.setWindowTitle('QDial') # 1 self.dial = QDial(self) # 实例化一个QDial控件后,通过setFixedSize()方法来固定QDial的大小 # 如果不设置该方法的话,我们会发现在改变表盘数值时,表盘的大小会发生改变; self.dial.setFixedSize(100, 100) # 2 # 使用setRange()方法来设置表盘数值范围, # 当然也可以使用setMinimum()和setMaximum()方法; self.dial.setRange(0, 100) # 3 # setNotchesVisible(True)可以显示刻度,刻度会根据我们设置的数值自动调整; self.dial.setNotchesVisible(True) # 4 # 当改变表盘数值时,会触发valueChanged信号,在槽函数中我们让QLabel显示出当前表盘数值。 self.dial.valueChanged.connect(self.on_change_func) # 5 self.label = QLabel('0', self) self.label.setFont(QFont('Arial Black', 0)) self.h_layout = QHBoxLayout() self.h_layout.addWidget(self.dial) self.h_layout.addWidget(self.label) self.setLayout(self.h_layout) def on_change_func(self): self.label.setText(str(self.dial.value())) self.label.setFont(QFont('Arial Black', (int(self.dial.value()) + 10)))
class MainWindow(QMainWindow): def __init__(self): super().__init__() # 윈도우 설정 self.setGeometry(200, 100, 300, 300) # x, y, w, h self.setWindowTitle('삼인성호') # QSliter 추가 self.dial = QDial(self) self.dial.move(10, 10) self.dial.setFixedSize(100, 100) self.dial.setRange(0, 100) self.dial.setMinimum(1) self.dial.setMaximum(24) self.dial.setNotchesVisible(True) self.dial.valueChanged.connect(self.value_changed) # QSlider 데이터를 표시할 라벨 self.label = QLabel(self) self.label.setGeometry(10, 120, 200, 100) self.label.setAlignment(Qt.AlignRight | Qt.AlignVCenter) self.label.setStyleSheet("border-radius: 5px;" "border: 1px solid gray;" "background-color: #ffffff") # 슬라이드 시그널 valueChanged 연결 함수 def value_changed(self, value): self.label.setText(str(value) + "시") self.label.setFont(QtGui.QFont("Malgun Gothic", 40)) #폰트,크기 조절
class Demo(QWidget): def __init__(self): super(Demo, self).__init__() self.setWindowTitle('QDial') # 1 self.dial = QDial(self) self.dial.setFixedSize(100, 100) # 2 self.dial.setRange(0, 100) # 3 self.dial.setNotchesVisible(False) # 4 self.dial.valueChanged.connect(self.on_change_func) # 5 self.label = QLabel('0', self) self.label.setFont(QFont('Arial Black', 20)) self.h_layout = QHBoxLayout() self.h_layout.addWidget(self.dial) self.h_layout.addWidget(self.label) self.setLayout(self.h_layout) def on_change_func(self): self.label.setText(str(self.dial.value()))
class QDialSlider(QWidget, QObject): '''New signals should only be defined in sub-classes of QObject. They must be part of the class definition and cannot be dynamically added as class attributes after the class has been defined.''' trigger = pyqtSignal() def __init__(self, name, maxV, bipolar=True, parent=None): super(QDialSlider, self).__init__(parent) self.overflow = False self.value = 0.0 if (maxV > 10): self.slider_multi = 10 else: self.slider_multi = 100 # Object self.edit = QDoubleSpinBox() self.dial = QDial() self.slider = QSlider(Qt.Horizontal) self.scale = QScale(maxV, bipolar) self.layout = self.CreateGroup(name) self.timer = QtCore.QTimer() # Property self.dial.setRange(-10, 10) self.slider.setTickInterval(100) self.edit.setSingleStep(0.01) self.edit.setDecimals(2) if bipolar: self.edit.setRange(-maxV, maxV) self.slider.setRange(int(-maxV * self.slider_multi), int(maxV * self.slider_multi)) else: self.edit.setRange(0, maxV) self.slider.setRange(0, int(maxV * self.slider_multi)) # Event self.slider.valueChanged.connect(self.SliderChange) self.edit.valueChanged.connect(self.EditChange) self.dial.sliderPressed.connect(self.DialPress) self.dial.sliderReleased.connect(self.DialRelease) self.timer.timeout.connect(self.onTimer) def setValue(self, dvalue): self.value = dvalue def setOverflow(self): self.overflow = True #layout and style def CreateGroup(self, name): self.edit.setFixedSize(98, 32) self.dial.setFixedSize(80, 80) #self.slider.setTickInterval(10) self.slider.setFocusPolicy(Qt.StrongFocus) self.slider.setTickPosition(QSlider.TicksBothSides) self.edit.setAlignment(Qt.AlignRight) self.edit.setFont(QFont("Arial", 16)) self.edit.setStyleSheet("color: rgb(36,36,36);") #layout hbox1 = QHBoxLayout() hbox1.addWidget(self.dial) hbox1.addWidget(self.edit, Qt.AlignCenter) vbox = QVBoxLayout() vbox.addLayout(hbox1) vbox.addWidget(self.scale) vbox.addWidget(self.slider) groupBox = QGroupBox(name) groupBox.setLayout(vbox) return groupBox @pyqtSlot() def SliderChange(self): if (self.slider.hasFocus()): val = self.slider.value() self.edit.setValue(val / self.slider_multi) #print('SliderChange'+str(val/self.slider_multi)) @pyqtSlot() def EditChange(self): val = self.edit.value() self.slider.setValue(int(val * self.slider_multi)) if (self.overflow): self.overflow = False else: self.trigger.emit() #print('EditChange'+str(val)) @pyqtSlot() def DialPress(self): self.timer.start(100) #print('DialPress') @pyqtSlot() def DialRelease(self): self.timer.stop() self.dial.setValue(0) #print('DialRelease') def onTimer(self): delta = self.dial.value() sign = lambda x: math.copysign(1, x) delta = sign(delta) * int((10**(abs(delta) / 4)) - 0.5) / 100 self.edit.setValue(self.edit.value() + delta)