Anterior: Cosas que nos alejan de ser una sociedad civilizada. Parte sexta, Eurovisión | Siguiente: Un nuevo deporte
Signals and slots
Python
One important aspect of adding a GUI to any code is that you must start thinking about events. Computations are easy from this point of view, either everything is serial or happens on a thread. With a GUI you must handle stuff like signals and slots.
Most of GUI libraries have some event handling primitives implemented, Qt uses signals and slots to connect events like pressing a button to a slot, that is, a function that is called when the event happens. I use Python to code GUIs so everything I'm saying from now on deals exclusively about Python.
A connection is a primitive that, needless to say, connects a signal to a slot. It has three arguments, the signal generator, the signal type and the slot. The slot is a function. More precisely a global function if you wan to avoid severe headaches trying to guess why your GUI is ignoring that function no matter how many times you press that button. There is a redundant question about this method. What can I do if I want to call a slot with arguments? A connection is between a signal and a function an there are no arguments present at any moment. I found an elegant solution this morning: use lambda functions in the connection calling. This will pre-eval the function with arguments and the connection won't see them.
-
Tags:
Ingeniería
One important aspect of adding a GUI to any code is that you must start thinking about events. Computations are easy from this point of view, either everything is serial or happens on a thread. With a GUI you must handle stuff like signals and slots.
Most of GUI libraries have some event handling primitives implemented, Qt uses signals and slots to connect events like pressing a button to a slot, that is, a function that is called when the event happens. I use Python to code GUIs so everything I'm saying from now on deals exclusively about Python.
A connection is a primitive that, needless to say, connects a signal to a slot. It has three arguments, the signal generator, the signal type and the slot. The slot is a function. More precisely a global function if you wan to avoid severe headaches trying to guess why your GUI is ignoring that function no matter how many times you press that button. There is a redundant question about this method. What can I do if I want to call a slot with arguments? A connection is between a signal and a function an there are no arguments present at any moment. I found an elegant solution this morning: use lambda functions in the connection calling. This will pre-eval the function with arguments and the connection won't see them.
