Files
Feeding_control_system/view/widgets/value_adjuster.py
2025-10-31 18:52:35 +08:00

145 lines
4.8 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from PySide6.QtWidgets import (QWidget, QHBoxLayout, QPushButton, QLineEdit,
QApplication)
from PySide6.QtCore import Qt
from PySide6.QtGui import QDoubleValidator
import sys
# 调整计划方量
class ValueAdjuster(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.min_value = 0 # 最小值
self.max_value = 99 # 最大值
self.value = 2.5 # 初始值
self.setFixedSize(102, 32)
# 创建控件
# 减号按钮
self.minus_btn = QPushButton("-")
self.minus_btn.setFixedSize(26, 26)
self.minus_btn.setCursor(Qt.PointingHandCursor)
# 中间的编辑栏
self.line_edit = QLineEdit(f"{self.value:.1f}") # 显示1位小数
self.line_edit.setFixedSize(40, 26)
# 加号按钮
self.plus_btn = QPushButton("+")
self.plus_btn.setFixedSize(26, 26)
self.plus_btn.setCursor(Qt.PointingHandCursor)
# 配置QLineEdit支持数字输入+居中显示
self.line_edit.setAlignment(Qt.AlignCenter) # 文本居中
# 限制输入为浮点数(支持负数,范围可自定义)
self.line_edit.setValidator(QDoubleValidator(0, 99, 1, self)) # 最多1位小数
self.line_edit.textChanged.connect(self.on_text_changed) # 监听输入变化
# 设置样式表(保持与按钮风格统一)
self.minus_btn.setStyleSheet("""
QPushButton {
background-color: #001c83;
color: #03f5ff;
border: 1px solid #039ec3;
font-size: 29px;
text-align: center;
padding-bottom: 4px;
}
QPushButton:hover {
background-color: #16ffff;
color: #00347e;
}
""")
# #03f5ff
self.line_edit.setStyleSheet("""
QLineEdit {
background-color: #001c83;
color: white;
border: 1px solid #039ec3;
font-size: 16px;
padding:0px;
font-weight: 560;
}
QLineEdit:focus {
border: 1px solid #039ec3;
outline: none;
}
""")
self.plus_btn.setStyleSheet("""
QPushButton {
background-color: #001c83;
color: #03f5ff;
border: 1px solid #039ec3;
font-size: 29px;
text-align: center;
padding-bottom: 4px;
margin: 0px;
}
QPushButton:hover {
background-color: #16ffff;
color: #00347e;
}
""")
# 连接信号槽(加减按钮)
self.minus_btn.clicked.connect(self.on_minus_clicked)
self.plus_btn.clicked.connect(self.on_plus_clicked)
# 设置布局间距0确保边框无缝衔接
layout = QHBoxLayout(self)
layout.setSpacing(2)
layout.setContentsMargins(0, 0, 0, 0) # 去除布局外边距
layout.addWidget(self.minus_btn)
layout.addWidget(self.line_edit)
layout.addWidget(self.plus_btn)
def on_minus_clicked(self):
"""减0.1"""
new_value = self.value - 0.1
# 限制最小值不低于min_value
if new_value >= self.min_value:
self.value = round(new_value, 1)
self.line_edit.setText(f"{self.value:.1f}")
def on_plus_clicked(self):
"""加0.1"""
new_value = self.value + 0.1
# 限制最大值不超过max_value
if new_value <= self.max_value:
self.value = round(new_value, 1)
self.line_edit.setText(f"{self.value:.1f}")
def on_text_changed(self, text):
"""监听输入框文本变化更新内部value"""
if not text:
return
try:
input_value = round(float(text), 1)
# 确保输入值在范围内
if self.min_value <= input_value <= self.max_value:
self.value = input_value
else:
# 超出范围时恢复到最近的有效 value
self.line_edit.setText(f"{self.value:.1f}")
except ValueError:
pass
# 获取具体的方量数值
def get_value(self):
return self.value
# 设置方量值
def set_value(self, value:float):
input_value = round(float(value), 1)
# 确保设置的值在范围内
if self.min_value <= input_value <= self.max_value:
self.value = input_value
self.line_edit.setText(f"{self.value:.1f}") # 更新方量值
if __name__ == "__main__":
app = QApplication(sys.argv)
widget = ValueAdjuster()
widget.show()
sys.exit(app.exec())