brisonus_app_eq/ERNCTuningTool/components/common/custom_widget.py
2025-02-18 22:05:52 +08:00

60 lines
1.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.QtCore import Signal
from PySide6.QtWidgets import QHBoxLayout, QLabel, QPushButton, QLineEdit, QDialog
class CustomWidget(QDialog):
signal_accepted = Signal(float)
def __init__(self, parent=None):
super().__init__(parent)
self.setAutoFillBackground(True)
# self.resize(280, 40)
# self.setStyleSheet("background-color:white;")
self.setModal(True)
# 自定义 Widget包含一个标签和一个文本框
self.layout = QHBoxLayout()
self.label = QLabel("输入数值:", self)
self.line_edit = QLineEdit(self)
# self.line_edit.setText() # 设置初始文本
self.push_button_set = QPushButton('', self)
self.push_button_cancel = QPushButton('', self)
self.layout.addWidget(self.label)
self.layout.addWidget(self.line_edit)
self.layout.addWidget(self.push_button_set)
self.layout.addWidget(self.push_button_cancel)
self.layout.setStretch(0, 1)
self.layout.setStretch(1, 2)
self.layout.setStretch(2, 1)
self.layout.setStretch(3, 1)
self.push_button_cancel.clicked.connect(self.close)
self.push_button_set.clicked.connect(lambda :self.accept())
self.setLayout(self.layout)
# 监听输入框内容变化
self.line_edit.returnPressed.connect(self.on_text_entered)
def set_input_val(self, text):
self.line_edit.setText(str(text))
def get_input_val(self):
try:
ret_val = float(self.line_edit.text())
return ret_val
except ValueError as e:
return None
def close(self):
self.hide()
def on_text_entered(self):
# 当回车键按下时,发出信号将文本返回
self.accept()
text_entered = Signal(str)