Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Event for custom toggle state change? #1

Closed
vladtimug opened this issue May 17, 2021 · 3 comments
Closed

Event for custom toggle state change? #1

vladtimug opened this issue May 17, 2021 · 3 comments

Comments

@vladtimug
Copy link

I am trying to figure out how to perform a certain action only when a slider is turned on. I looked at the class definition but I was not able to figure out how to listen for events on that object. Could you offer some input on this?

@kadir014
Copy link
Owner

If you meant ToggleSwitch it has a toggled signal, you can use it like this:

from pyqt5Custom import ToggleSwitch

...

tg = ToggleSwitch()

# this slot is connected to 'toggled' signal
def slot():
    if tg.isToggled():
        print("the toggle switch is on!")
        
    else:
        print("the toggle switch is off!")
        
tg.toggled.connect(slot)

@vladtimug
Copy link
Author

vladtimug commented May 19, 2021

That works thank you. However isn't it a bit odd that the slot function can only be defined before it can actually be used? Otherwise the mechanism will crash. One other thing would be, how would you use this with classes? Giving that you have a initUI method of a class which handles all graphical elements on the window how could one use the slot mechanism outside of the initUI method sfn the custom widgets inside of that initUI method? From my trials I could not get it to work. :)

@kadir014
Copy link
Owner

kadir014 commented May 19, 2021

However isn't it a bit odd that the slot function can only be defined before it can actually be used?
It actually depends on how you use signals & slots, of course you can't give the `slot` function to the signal before defining it. You can't use any object without defining them, otherwise you will get `'object' is not defined` error. However if you got any other error it is best to share the source code and the error traceback.


how could one use the slot mechanism outside of the initUI method
Unfortunately I couldn't understand what you meant here, but signal & slot system is not a new thing in my library. You can check out these links for further information on using signals and slots in PyQt5.

EDIT: Formatting

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants