Unsolved How to check if a signal is triggered from GUI or Code
-
I have a scenario, where i have process a signal triggered from GUI and block the same signal if triggered from Code base.
Eg: I am subclassing QListWIdget,
In Constructor
connect(this,SIGNAL(itemChanged(QListWidgetItem*)),this,SLOT(itemChangedSlot(QListWidgetItem*))); }
In this case, the following slot gets called if changes done on ListItem in UI, as well as when setText() is called from code
void myListWidget::itemChangedSlot(QListWidgetItem * item) { if(.....) { .... } else { item->setText("hello")); } }
Is there a way to find, if particular signal is triggered from UI or from code ??
My requirement is that i need to process the signal only if its from UI, and block the signal from calling the slot if its from code.
Any help is appreciated.
Thanks for reading.
-
@Vinoth-Rajendran4 said in How to check if a signal is triggered from GUI or Code:
My requirement is that i need to process the signal only if its from UI, and block the signal from calling the slot if its from code.
- set a member flag variable before you execute code that triggers the signal which you can check in your slot, and revert it after the code
- call
QObject::blockSignals()
before and after the your code which triggers the signal, but note that this blocks all signals of the object. Depending on the object/signal this might have side effects for Qt's internal code
-
@raven-worx : Tried QObject::blockSignals(). Suits my need.
Thank u -
@raven-worx : But is there any way to find out on entry of slot function, that particular signal is triggered from UI or code ??
-
@Vinoth-Rajendran4
no not really.
In the end everything is triggered from code ;)
"From UI" mostly means that somewhere in the call stack a (input-)event caused the code to be executed. -
@Vinoth-Rajendran4 said in How to check if a signal is triggered from GUI or Code:
But is there any way to find out on entry of slot function, that particular signal is triggered from UI or code ??
It is possible to know which was the signal sender with sender() and the signal index with senderSignalIndex()
-
Hi,
If you mean "direct call" VS "signal/slots" then the
sender()
return value will be 0 in the first case and whatever sent the signal in the second case.