Sending multiple signals at once
-
I have a left widget and a right widget in my mainWindow. I am populating the left widget with some push buttons(say valves) in a grid layout. When I press the clean button with value 5, I want 5 valves to be clicked simultaneously and turn green. I also want the functionality that the valves should turn green when clicked individually. For this, I have a onValveClicked() slot in valve.cpp. This function just turn the valves green. So, I think I would need to send 5 signals simultaneously so that all those 5 valves turn green at the same time. Can someone tell me if this approach is plausible and correct?
-
I have a left widget and a right widget in my mainWindow. I am populating the left widget with some push buttons(say valves) in a grid layout. When I press the clean button with value 5, I want 5 valves to be clicked simultaneously and turn green. I also want the functionality that the valves should turn green when clicked individually. For this, I have a onValveClicked() slot in valve.cpp. This function just turn the valves green. So, I think I would need to send 5 signals simultaneously so that all those 5 valves turn green at the same time. Can someone tell me if this approach is plausible and correct?
What's the actual problem then? Simply emit those five signals and connect them to the appropriate slot(s) to change the color.
-
What's the actual problem then? Simply emit those five signals and connect them to the appropriate slot(s) to change the color.
@Christian-Ehrlicher
Thank you for your response!
I was giving 5 as a reference. Consider for example, if there are 70 valves and I am giving an input 5, then 1-5 valves will be clicked and will turn green, then 6-10 will be clicked and so on. So, I just needed to know if giving multiple signals at once is possible, because 1-5 have to be clicked at the same time. Or is there any other approach possible? -
@Christian-Ehrlicher
Thank you for your response!
I was giving 5 as a reference. Consider for example, if there are 70 valves and I am giving an input 5, then 1-5 valves will be clicked and will turn green, then 6-10 will be clicked and so on. So, I just needed to know if giving multiple signals at once is possible, because 1-5 have to be clicked at the same time. Or is there any other approach possible?@shreya_agrawal said in Sending multiple signals at once:
because 1-5 have to be clicked at the same time.
What does 'at the same time' means?
I don't see any problem here - just emit the signals and properly connect them to the slots - what else do you want to do?
-
@Christian-Ehrlicher
Thank you for your response!
I was giving 5 as a reference. Consider for example, if there are 70 valves and I am giving an input 5, then 1-5 valves will be clicked and will turn green, then 6-10 will be clicked and so on. So, I just needed to know if giving multiple signals at once is possible, because 1-5 have to be clicked at the same time. Or is there any other approach possible?@shreya_agrawal
Nothing is really going to happen "at once". Each signal emitted waits for all attached slots to complete, next signal cannot be emitted till them. (At least in default, nonqueued-mode, assuming you are not emitting across threads.)Depending on how efficient you wish to be. And depending on whether you you really need to fire the individual buttons' signals or whether you just need to turn them green. You could emit your own signal, with whatever parameters, and have a single slot attached to that. That slot could call the
click()
signal on each button: that still fires each button's signal in turn, but it saves the outside world having to access and do so on each one. You can write all your logic in this slot, for handling multiple buttons etc. So the outside world only has to emit one signal you define, perhaps with a parameter.If you do not need to execute whatever slots would normally fire on each button being clicked but only "turn them green", you could call whatever you do for that directly if you don't need to go via
click()
slot/clicked()
signal. -
I have a left widget and a right widget in my mainWindow. I am populating the left widget with some push buttons(say valves) in a grid layout. When I press the clean button with value 5, I want 5 valves to be clicked simultaneously and turn green. I also want the functionality that the valves should turn green when clicked individually. For this, I have a onValveClicked() slot in valve.cpp. This function just turn the valves green. So, I think I would need to send 5 signals simultaneously so that all those 5 valves turn green at the same time. Can someone tell me if this approach is plausible and correct?
@shreya_agrawal
Yes. Put the valve objects in a vector and then connect signals and slots.I am typing on a phone, sorry.
for( int i=0; ... to 70) { connect(cleanButton, QPushButton::click, vector_of_valve_pointers[i], valveClass::turnGreen) }
Like this you make the connection when the clean pushbutton is created.
You can also dynamically establish connection and terminate them because for each new connection you get an id. So, when a valve doesn't need to be cleaned, you destroy the connection. Or, instead of having a for loop of 70 steps, I'd rather make a new connection as soon as a valve becomes dirty.
-
@shreya_agrawal
Nothing is really going to happen "at once". Each signal emitted waits for all attached slots to complete, next signal cannot be emitted till them. (At least in default, nonqueued-mode, assuming you are not emitting across threads.)Depending on how efficient you wish to be. And depending on whether you you really need to fire the individual buttons' signals or whether you just need to turn them green. You could emit your own signal, with whatever parameters, and have a single slot attached to that. That slot could call the
click()
signal on each button: that still fires each button's signal in turn, but it saves the outside world having to access and do so on each one. You can write all your logic in this slot, for handling multiple buttons etc. So the outside world only has to emit one signal you define, perhaps with a parameter.If you do not need to execute whatever slots would normally fire on each button being clicked but only "turn them green", you could call whatever you do for that directly if you don't need to go via
click()
slot/clicked()
signal.@JonB
By the approach you mentioned, I can turn multiple valves green at the same time, but I also want to click them simultaneously, while turning them green. If I pass the clicked() signal in a loop in my turnGreen slot, then this signal will go individually for each valve. But I want all those valves to be clicked at once. Because all those signals have to go to the PLC at once, so that those valves can be cleaned. I hope I am able to clearly put forward my use case. -
@JonB
By the approach you mentioned, I can turn multiple valves green at the same time, but I also want to click them simultaneously, while turning them green. If I pass the clicked() signal in a loop in my turnGreen slot, then this signal will go individually for each valve. But I want all those valves to be clicked at once. Because all those signals have to go to the PLC at once, so that those valves can be cleaned. I hope I am able to clearly put forward my use case.There is nothing like 'at once' - it's done one after the other. When you want to pass a state to something else then you have to accumulate the result (= wait some time). This is nothing Qt specific here and for sure has nothing to do with signals and slots.
-
Hi,
Then you are tying too much of the business logic to your UI.
Taking the GUI aside, how would you send the command to your PLCs so they start doing their stuff at the same time ?
-
@JonB
By the approach you mentioned, I can turn multiple valves green at the same time, but I also want to click them simultaneously, while turning them green. If I pass the clicked() signal in a loop in my turnGreen slot, then this signal will go individually for each valve. But I want all those valves to be clicked at once. Because all those signals have to go to the PLC at once, so that those valves can be cleaned. I hope I am able to clearly put forward my use case.By the way: Since you are talking about "at once".
If the only thing you do is turning your valve buttons green and do some other "cheap" tasks, the human eye wont see any difference if this happens really "at once" (litterally same time) or some milisecs apart when signals are processed sequentially -
By the way: Since you are talking about "at once".
If the only thing you do is turning your valve buttons green and do some other "cheap" tasks, the human eye wont see any difference if this happens really "at once" (litterally same time) or some milisecs apart when signals are processed sequentially@Pl45m4
Thank you for your response !
Yes, I tried passing the signals in the for loop and its actually not visible to the naked eye, so I will go forward with that method. -