QPushButton toggled connection does not seems to be trigger at the start

18 views
Skip to first unread message

kiteh

unread,
Dec 12, 2018, 7:10:49 PM12/12/18
to Python Programming for Autodesk Maya
Hi everyone, I have an issue with the use of the `toggled` signal.
I loaded and created the GUI using .ui or uic module.

Example of my tool's code:
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("+")


As I load my tool, I had expected the button text to be '-' but instead it shows up as '+', even though the frame is still shown.

I had thought that by using toggled and having it initialized under the __init__(), it will reads both setInfoVis() and toggleVisibility() but when I tried adding in a couple of print statements, it does not seems to even go pass the `if` statement unless I clicked on the button, only will the print statements within the `if` will be executed, or if I added in setInfo() before the `xxx.toggled.connect()...`

Any ideas? Or any way that I can force the toggleVisibility() to be executed on started up?

Justin Israel

unread,
Dec 12, 2018, 8:40:26 PM12/12/18
to python_in...@googlegroups.com
On Thu, Dec 13, 2018 at 1:10 PM kiteh <kiteh...@gmail.com> wrote:
Hi everyone, I have an issue with the use of the `toggled` signal.
I loaded and created the GUI using .ui or uic module.

Example of my tool's code:
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("+")


As I load my tool, I had expected the button text to be '-' but instead it shows up as '+', even though the frame is still shown.

I had thought that by using toggled and having it initialized under the __init__(), it will reads both setInfoVis() and toggleVisibility()

This part is the misconception: "it will read both setInfoVis and toggleVisibility)

That isn't actually how signals/slots work. The signals don't read from the slots. The signals trigger the slots. This occurs by Qt being told to emit a signal and then iterating the registered slots and calling them. No signal, no slot. Your initial state of your class does not emit and signals after you have wired up the toggled signal to a slot. 

If you did something like this in your constructor, it would fix your problem:

    # class state is initialized from ui file and other custom settings
    # ...

    self.setInfoVis()

    # custom signals
    self.informationVisBtn.toggled.connect(self.setInfoVis)
 
but when I tried adding in a couple of print statements, it does not seems to even go pass the `if` statement unless I clicked on the button, only will the print statements within the `if` will be executed, or if I added in setInfo() before the `xxx.toggled.connect()...`

Any ideas? Or any way that I can force the toggleVisibility() to be executed on started up?

See above.
 
Also I just wanted to point out that it is a bit redundant, at least from your small example, to have the setInfoVis() which calls toggleVisibility(value) when the toggled signal from the button emits with a bool argument and could be wired directly to toggleVisibility


--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/dede49fa-6633-4fff-a7b6-ea4d31254098%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages