我有一个Qgroupbox,其中包含带有Qlabels的Qcombobox,我想从Qcombobox中选择一个值,并将该值显示为Qlabel。我有完整的代码,即使我在函数内的前后都可以打印值,也可以正常工作,仅显示setText
不会将文本设置为Qlabel并进行更新。
当前屏幕
我想要的
我已经纠正了信号代码,当其中出现Qgroupbox
的{{1}}或值将被更改时,Qcombobox
将发出索引的self.activation.connect(......)
。为了确保能正常工作,我在int
内打印it-value
,请参见下图,它确实有效,然后将参数传递给函数def setdatastrength(self, index)
并在{内进行值打印{1}}确保值传递,如下图所示,它确实起作用。此行代码self.concreteproperty.display_condata(it)
不会为Qlabel赋值。
脚本
def display_condata(self, value)
以上代码已更正并最终生效。现在它可以正常工作了。
答案 0 :(得分:1)
我认为问题在于您的接收插槽与任何可用的.activated
信号都不匹配。
self.activated.connect(self.setdatastrength)
@QtCore.pyqtSlot()
def setdatastrength(self):
index = self.currentIndex()
it = self.compressive_strength[index]
print(it)
self.concreteproperty.display_condata(it)
QComboBox.activated
信号发出索引的int
或选定值的str
。 See documentation。
您已将其附加到setdatastrength
上,该{accepts不接受任何参数(除self
之外,对象中的任何参数)—这意味着它与任何可用信号的签名都不匹配,并且不会被调用。如果您更新定义以添加索引值,并接受单个int
,则它应该起作用。
self.activated.connect(self.setdatastrength)
@QtCore.pyqtSlot(int) # add the target type for this slot.
def setdatastrength(self, index):
it = self.compressive_strength[index]
print(it)
self.concreteproperty.display_condata(it)
更新后-上面的内容现在似乎已解决,尽管您不需要index = self.currentIndex()
中的附加setdatastrength
,但这没有任何害处。
查看您的代码,我认为标签正在正在更新。实际上,问题在于您根本看不到标签。查看ConcreteProperty
class ConcreteProperty(QtWidgets.QWidget):
def __init__(self, parent=None):
super(ConcreteProperty, self).__init__(parent)
self.setFont(QtGui.QFont("Helvetica", 10, QtGui.QFont.Normal, italic=False))
self.concretestrength_lay = QtWidgets.QHBoxLayout()
fctd = "\nfcd\n\nfctd\n\nEc"
con_strength = QtWidgets.QLabel(fctd)
self.con_strength_value = QtWidgets.QLabel(" ")
self.concretestrength_lay.addWidget(con_strength)
self.concretestrength_lay.addWidget(self.con_strength_value, alignment=QtCore.Qt.AlignLeft)
未显示更改的原因是您创建了两个 ConcreteProperty
对象,一个在ConcreteStrengthInfo
中,一个在ConcreteStrengthComboBox
中。对组合框的更新将触发对附加到组合框的ConcreteProperty
的更新,而不是其他更新(它们是单独的对象)。可见的ConcreteProperty
不受影响。
要执行此操作,您需要将信号附件+插槽移出组合框对象。以下是这两个部分的替换—
class ConcreteStrengthComboBox(QtWidgets.QComboBox):
def __init__(self, parent = None):
super(ConcreteStrengthComboBox, self).__init__(parent)
self.addItems(["C12/15","C16/20","C20/25","C25/30","C30/37","C35/45","C40/50","C45/55",
"C50/60","C55/67","C60/75","C70/85","C80/95","C90/105"])
self.setFont(QtGui.QFont("Helvetica", 10, QtGui.QFont.Normal, italic=False))
self.compressive_strength = ["12","16","20","25","30","35","40","45","50","55",
"60","70","80","90"]
class ConcreteStrengthInFo(QtWidgets.QWidget):
def __init__(self, parent=None):
super(ConcreteStrengthInFo, self).__init__(parent)
hbox = QtWidgets.QHBoxLayout()
concrete_strength = QtWidgets.QLabel("Concrete strength: ")
hbox.addWidget(concrete_strength)
self.concreteproperty = ConcreteProperty()
self.concretestrengthbox = ConcreteStrengthComboBox()
hbox.addWidget(self.concretestrengthbox)
self.concretestrengthbox.activated.connect(self.setdatastrength)
self.vlay = QtWidgets.QVBoxLayout()
self.vlay.addLayout(hbox)
self.vlay.addLayout(self.concreteproperty.concretestrength_lay)
@QtCore.pyqtSlot(int)
def setdatastrength(self, index):
it = self.concretestrengthbox.compressive_strength[index]
print(it)
self.concreteproperty.display_condata(it)
这在本地对我有效。