Results 1 to 5 of 5

Thread: Changing the slot of a signal

  1. #1
    Join Date
    Jun 2016
    Location
    Southwest Virginia
    Posts
    4
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11

    Default 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:
    Screen Shot 2016-06-29 at 13.48.50.jpg
    Here's some heavily abridged code it runs on:

    Qt Code:
    1. class MyWidget(QWidget):
    2. def __init__(self):
    3. super().__init__()
    4. self.main_screen()
    5.  
    6. def main_screen(self):
    7. self.button_1 = RightSideButton("Enter")
    8. # RSB is my own class, to normalize the size of the buttons to 1 inch square on screen, extending QPushButton
    9. self.main_input = QLineEdit()
    10. # sets everything else up
    11.  
    12. self.reset()
    13. self.showMaximized()
    14.  
    15. def reset(self):
    16. # does stuff
    17. self.button_1.clicked.connect(self.run_main)
    18. self.main_input.returnPressed.connect(self.button_1.click)
    19.  
    20. def run_main(self):
    21. # does stuff
    22. self.button_1.clicked.connect(self.run_next) # this line doens't seem to do anything
    23.  
    24. def run_next(self):
    25. # does stuff
    26. self.reset()
    To copy to clipboard, switch view to plain text mode 

    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?

  2. #2
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default 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.
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

  3. #3
    Join Date
    Jun 2016
    Location
    Southwest Virginia
    Posts
    4
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11

    Default 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.

  4. #4
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Changing the slot of a signal

    Quote Originally Posted by hamstap85 View Post
    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:

    Qt Code:
    1. def run_main(self):
    2. # does stuff
    3. self.button_1.clicked.connect(self.run_next)
    4. self.button_1.clicked.disconnect(self.run_main)
    To copy to clipboard, switch view to plain text mode 
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

  5. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default 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,
    _

Similar Threads

  1. Replies: 1
    Last Post: 14th August 2014, 18:08
  2. Replies: 6
    Last Post: 4th March 2014, 16:09
  3. Replies: 8
    Last Post: 7th November 2012, 15:10
  4. Replies: 6
    Last Post: 22nd August 2010, 00:12
  5. signal slot conection using a string, not a SLOT
    By rianquinn in forum Qt Programming
    Replies: 6
    Last Post: 5th February 2006, 19:52

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.