How to avoid Confliction due to “Same signal, different slots” in Qt?
-
I am working on an application which has 3 plug-ins. The main application is a Qt project and along with it we also add 3 more Qt projects which are the plug-ins to the main application.
I am calling a connect statement in one project where the SIGNAL is triggered(), the object is shown below and has a slot slt_menuSetIngestDir(). I am calling a connect statement in another project which has the same object, same SIGNAL triggered() but has a different slot slt_IngestDir().
Here is the sample connect statement in Project 1:
@connect(PluginLocalManager::getInstance()->getEventIF()->getMenuAction(M_TOOL_SETINGESTDIR), SIGNAL(triggered()), this, SLOT(slt_menuSetIngestDir()) );@Sample Connect statement in Project 2:
@connect(PluginLocalManager::getInstance()->getEventIF()->getMenuAction(M_TOOL_SETINGESTDIR), SIGNAL(triggered()), this, SLOT(slt_IngestDir()) );@The problem is when I run the application, and when the click the particular option, it triggers the SIGNAL using the same object and calls two different slots at the sametime.
Is it possible that I can disconnect a signal in Project 2 and make sure my connect statement in Project 1 works specifically for Project 1 and vice versa??? I have come across disconnect statement in Qt but It looks like I am missing something. Please help!!
-
If the same object (same pointer in memory) emits the signal, and you connect it to two slots, don't be surprised it will fire both slots. If they are two different instances of the same class, then this should not be happening. But I think I misunderstood you.
-
Thanks for the reply Sierdzio. The object in both connect statements is the instance of a class. In simple words the same object(same pointer in memory). Does disconnect() help???
[quote author="sierdzio" date="1362561772"]If the same object (same pointer in memory) emits the signal, and you connect it to two slots, don't be surprised it will fire both slots. If they are two different instances of the same class, then this should not be happening. But I think I misunderstood you.[/quote]
-
Yes, but is this the same instance?
-
Yes Sierdzio its the same instance.
[quote author="sierdzio" date="1362562462"]Yes, but is this the same instance?[/quote]
-
OK, then the signal will be received by both slots. You could do some magic with QObject::sender() if you wish, but probably you just need to readjust to the reality of meta objects ;)
-
Well I think QObject::sender() makes sense. How can one make sure that using QObject::sender(), the slot of the same project gets called and doesnt call the other slot?
[quote author="sierdzio" date="1362562752"]OK, then the signal will be received by both slots. You could do some magic with QObject::sender() if you wish, but probably you just need to readjust to the reality of meta objects ;)[/quote]
-
Easiest would be simply not to connect the slot :) Since we are talking about the same instance, ::sender() will be the same in both cases. If anything, you need to modify the design of your app, or modify slot code. To be honest, I don't understand what you want to achieve, so it's a bit hard to help.
-
Maybe it would be easier to have 2 different signals, it's hard to understand why there are 2 slots for the same signal if you don't want to call the both slots when the signal is emited.
Regards.