QPushButton切换的连接在s处似乎没有触发

2024-07-03 05:32:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在连接一个QPushButton,它可以在一个框架内隐藏/显示小部件。

我使用^{cd1>}方法加载/创建了GUI。

对于这个QPushButton,我已经设置并检查了属性setChecked

class MyWindow(QtGui.QWidget):
    def __init__(self):
        ...
        # self.informationVisBtn, `setChecked` and `setCheckable` field is checked in the .ui file
        self.informationVisBtn.toggled.connect(self.setInfoVis)

    def setInfoVis(self):
            self.toggleVisibility(
                self.informationVisBtn.isChecked()
            )

    def toggleVisibility(self, value):
        if value:
            self.uiInformationFrame.show()
            self.informationVisBtn.setText("-")
        else:
            self.uiInformationFrame.hide()
            self.informationVisBtn.setText("+")

在第一次尝试加载代码时,我注意到^{cd3>},当选中它时,将显示框架,但文本没有设置为^{{cd4>},而是保留为my.ui文件中设置的^{cd5>}。

除非在^{cd6>}中,如果在设置连接之前添加^{cd7>}中,则只有正确填充文本。

^{cd8>}的使用是否在开始时触发状态?如有任何答复,请提前感谢。


Tags: 文本self框架uivalue部件defsettext
1条回答
网友
1楼 · 发布于 2024-07-03 05:32:59

信号在状态发生变化时发出,并通知连接到该时刻的插槽。连接新插槽时,只有在连接后状态发生变化时才会通知它,因此建议始终将状态更新为信号。另一方面,不需要创建setInfoVis()方法,因为toggle传递状态信息。在

class MyWindow(QtGui.QWidget):
    def __init__(self):
        super(MyWindow, self).__init__()
        # ...
        self.informationVisBtn.toggled.connect(self.toggleVisibility)

        # update the state it has since the connection
        # was made after the state change
        self.toggleVisibility(
                self.informationVisBtn.isChecked()
            )

    @QtCore.pyqtSlot(bool)
    def toggleVisibility(self, value):
        self.uiInformationFrame.setVisible(value)
        self.informationVisBtn.setText("-" if value else "+")

相关问题 更多 >