我正在使用pyqt4和python制作GUI。现在我有一个QLineEdit和QComboBox,其中QLineEdit显示值,QComboBox可用于更改单位。我正在使用信号和插槽来处理用户的实时单位/值反馈,但我在理解如何以编程方式处理值时遇到问题,因为我需要它们都是标准单位。这是我到目前为止所做的,combo_box_line_edit_list是一个列表,我将组合框和行列表包装在一起
class UnitConverterSignaler(QtCore.QObject):
def __init__(self, combo_box_line_edit_list):
super(QtCore.QObject, self).__init__()
self.combo_box_line_edit_list = combo_box_line_edit_list
self.combo_box_list = [line_edit_combo_box[0] for line_edit_combo_box in combo_box_line_edit_list]
for combo_box, line_edit in self.combo_box_line_edit_list:
combo_box.currentIndexChanged['QString'].connect(line_edit.convert_units)
line_edit.store_unit_state(combo_box.currentText())
line_edit.standard_unit = combo_box.itemText(1)
def convert_to_standard(self):
for combo_box in self.combo_box_list:
combo_box.setCurrentIndex(0)
def convert_to_international(self):
for combo_box in self.combo_box_list:
combo_box.setCurrentIndex(1)
def toggle_unit_conversion(self, hold_line_values_steady):
for combo_box in self.combo_box_list:
if hold_line_values_steady:
combo_box.do_not_convert_units_on_change()
else:
combo_box.convert_units_on_change()
def convert_units_on_change(self):
"""
Changes the value of the line edit each time the combo box is changed
"""
for combo_box, line_edit in self.combo_box_line_edit_list:
combo_box.currentIndexChanged['QString'].connect(line_edit.convert_units)
combo_box.currentIndexChanged['QString'].disconnect(line_edit.store_unit_state)
def do_not_convert_units_on_change(self):
"""
Holds the line edit value constant in spite of combo box changes
"""
for combo_box, line_edit in self.combo_box_line_edit_list:
combo_box.currentIndexChanged['QString'].disconnect(line_edit.convert_units)
combo_box.currentIndexChanged['QString'].connect(line_edit.store_unit_state)
实例化&用于另一个类
self.lockCellCheckBox.toggled.connect(self.unit_converter_signaler.toggle_unit_conversion)
self.internationalPushButton.clicked.connect(self.unit_converter_signaler.convert_to_international)
self.standardPushButton.clicked.connect(self.unit_converter_signaler.convert_to_standard)
我也修补了QLineEdit而不是子类,所以我可以使用QtDesigner进行快速更改。
# monkey patch slot onto line_edit
def convert_units(line_edit, end_unit):
converted_unit_value = line_edit.unit_registry.convert(float(line_edit.text()), line_edit.stored_unit_state, str(end_unit))
line_edit.setText(str(converted_unit_value))
line_edit.stored_unit_state = str(end_unit)
# monkey patch slot onto line_edit
def store_unit_state(line_edit, unit):
line_edit.stored_unit_state = str(unit)
在我的主程序中获得标准单位的最通用方法是在UnitConverter中为每个组合框/行编辑创建信号吗?
答案 0 :(得分:1)
根据我目前的理解:您有许多组合框/行编辑对,输入的值应始终转换为标准单位(例如,显示在第三个QLabel或其他任何内容上)。
在我的主程序中获得标准单位的最通用方法是在UnitConverter中为每个组合框/行编辑创建信号吗?
不,你没必要。 python中的插槽(或特别是pyqt中)可以是任何可调用对象。可调用对象是实现方法__call__(self)
的对象
因此,我建议您创建一个类,它将相关对象作为参数在构造函数中进行,并在__call__(self)
中更改它们。像这样:
class ConverterSignal:
def __init__(whatever_you_want_to_refer_to):
self.whatever_you_want_to_refer_to = whatever_you_want_to_refer_to
def __call(self)__:
""" Here you can refer to whatever_you_want_to_refer_to and do whatever you want with it """
连接完成如下(对于组合框作为示例):
self.connect(combo_box, QtCore.SIGNAL('activated(int)'), ConverterSignal(whatever_you_want_to_refer_to))
这里创建了一个类ConverterSignal
的实例,如果发出相应的信号,将会调用它。