View Full Version : how to create own event?

30th July 2019, 14:24
Hello there,

I want to create own event, there is a real button and get "1","0" signal from it. If push the button, i will get signal 1 and then i will change text color. How can i do that

This problem seems like basic but how can i check real button's state continuously? i need to create event like pushButton.clicked.connect(). it will work only when the actual button is pressed.

30th July 2019, 17:24
What does it mean "there is a real button" ? Is it a hardware button ?

30th July 2019, 17:33
i need to create event like pushButton.clicked.connect()

This is not an "event".

Are you saying that you have a real, physical button that a human pushes with her finger, and you want to detect that press with your program? I assume you are still using a RasPi as you mentioned in your previous post.

First, you need to use the functions of python on the RasPi to detect the physical state change of the button. There are many examples for how to detect contact closures using the input pins of the Pi. I am not sure if you need to monitor the button state continuously or whether the Pi can send an interrupt to your program when the state changes. If you cannot use an interrupt, then I would suggest you use a QTimer to check the button every 100 ms or so in a slot connected to the QTimer's timeout() signal. If you write a loop to continually check the state, then your program will freeze in the loop.

You should implement the code for responding to the button press in a class derived from QObject. Classes based on QObject have the ability to send signals. In your class, you should implement a signal that will be emitted when the button changes state. You should also be able to find examples of how to implement a custom signal in your PyQt documentation. You can then connect this signal to any other part of your program that needs to know when the button has been pressed.

2nd August 2019, 08:21
Thank you so much,sir. Thats working.