1 Attachment(s)
Changing the slot of a signal
(PyQt5)
I have my first gui program that is in the VERY rough stages. Here's what it looks like just for reference:
Attachment 12017
Here's some heavily abridged code it runs on:
Code:
def __init__(self):
super().__init__()
self.main_screen()
def main_screen(self):
self.button_1 = RightSideButton("Enter")
# RSB is my own class, to normalize the size of the buttons to 1 inch square on screen, extending QPushButton
# sets everything else up
self.reset()
self.showMaximized()
def reset(self):
# does stuff
self.button_1.clicked.connect(self.run_main)
self.main_input.returnPressed.connect(self.button_1.click)
def run_main(self):
# does stuff
self.button_1.clicked.connect(self.run_next) # this line doens't seem to do anything
def run_next(self):
# does stuff
self.reset()
In this, button_1 is the one on the bottom right. I'm trying to have the program run through different "modes" as it were, where in each mode it does different things when you type something in the main input and hit enter or the button on the lower right.
It works at first, going through run_main() as it should, but when it gets to the end (line 21) and tries to "change modes" by changing the slot of the button signal, it doesn't work and it just goes back through run_main() if I hit enter in the main input OR click the button. Is there even a way to change the slot of a signal once it's been set?
Re: Changing the slot of a signal
Connecting a a new signal/slot does not replace any existing signals/slots. If I understand what you're trying to do, you need to disconnect the signal/slot in run_main after you've connected the clicked signal with the run_next slot, otherwise, next button click, all connected slots will be called, which means your run_main slot would be invoked again, which I suspect is what you're encountering.
Re: Changing the slot of a signal
That does seem to be the case, because I put in some print debugging messages to go along with breakpoints, and it does run_next after run_main
So now to discover how to clear the slots.
Re: Changing the slot of a signal
Quote:
Originally Posted by
hamstap85
So now to discover how to clear the slots.
You use the disconnect method instead of the connect method. I don't write python code, but I presume the syntax would be something like:
Code:
def run_main(self):
# does stuff
self.button_1.clicked.connect(self.run_next)
self.button_1.clicked.disconnect(self.run_main)
Re: Changing the slot of a signal
You could also keep a single connection but connect to a slot which then calls the right method depending on your application's state.
Cheers,
_