Reputation: 455
I am connecting a QPushButton in which it will either hide/ show the widgets within a Frame.
I loaded/ created my GUI using the .ui
method.
For this QPushButton, I have set and checked the attribute 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("+")
While loading my code on the first try, I noticed that the informationVisBtn
, while it is checked, the frame is being shown but the text did not gets set to -
and instead it remains as a +
as set in my .ui file.
Unless in the __init__()
, if I add in setInfoVis()
before setting the connection, only will the text be populated correctly.
Does the use of toggled
not trigger the state at the start? Appreciate in advance for any replies.
Upvotes: 1
Views: 958
Reputation: 243887
The signal is emited when there is a change of state and notify the slots that are connected up to that moment. When a new slot is connected, it will only be notified if there is a change of status after the connection so it is always advisable to update the status as signals. On the other hand it is not necessary to create setInfoVis() method since toggled transmits the state information.
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 "+")
Upvotes: 2