PDA

View Full Version : Reconnect signal/slot between two objects



MadBear
5th October 2010, 15:19
Greetings,
I have stumbled on a strange problem. I have multiple QGraphicsScene objects from which signals from only one should be connected to slots of some buttons on user interface. So when I change scenes I disconnect old scene and connect new scene. So far it works. But if I return to previous scene (back button), signal/slot connections do not function anymore. Is there something in QObject::connect and QObject::disconnect that I am missing?
Thank you.

Regards,
MadBear

Lykurg
5th October 2010, 15:28
what does the "back button"? did you also change the connections?

MadBear
5th October 2010, 15:58
Greetings,
Thank you for your reply. Ok buttons that move between scenes do (in a nutshell):


disconnect(currentScene,0,0,0);
currentScene=nextScene(); //or previousScene()
connect(currentScene,SIGNAL(),control,SLOT());


This happens every time I press button (OK actually there are forward and back button). The problem arises when I return to scene that was already connected to control. Then signal/slot connection does not work.

Regards,
MadBear

Lykurg
5th October 2010, 16:28
if forward works and backwards is not, then you have an error somewhere in your code. (The scene doesn't get blocked all the time if you once disconnect it.) Did you use the same functions to connect for both cases. Are you using a valid pointer to the "previous scene"?

MadBear
5th October 2010, 16:41
Greetings,
Thank you for your reply. Now to answer your question. I use this disconnect and connect on forward and back buttons. And pointers are valid (I store them in QList). I disconnect with pointer to scene that was currently active, then I put pointer to next scene (or previous) into variable currentScene and connect this new current scene to slot.
I hope I can show this in a pseudocode:



void init()
{
currentScene=list.someScene();
connect(currentScene,SIGNAL(),control,SLOT());
}
...
...
...
void forwardButtonClick()
{
disconnect(currentScene,0,0,0);
currentScene=list.nextScene();
connect(currentScene,SIGNAL(),control,SLOT());
}

void backButtonClick()
{
disconnect(currentScene,0,0,0);
currentScene=list.previousScene();
connect(currentScene,SIGNAL(),control,SLOT());
}


Like I already said, the problem arises when I click for instance forwardButton and then backButton. After that signals do not work anymore (even if I would press forward again).

Regards,
MadBear

Lykurg
5th October 2010, 17:02
Well, the pseudo code does not help very much, since in theory it should work. So can you provide a minimal compilable example which reproduce your problem?

MadBear
6th October 2010, 06:26
Greetings,
Thank you for your help. I must admit, that this problem showed me again, that stupid errors are sometimes most difficult to detect. There was another signal in the scene which was created in scene constructor (it was simple signal/slot connection on itself that in turn emits correct signals to the outside (not the most elegant solution)) and it was deleted with disconnect(scene,0,0,0). I completely forgot about that signal.
Thank you again for your help and my apologies for waste of your time.

Regards,
MadBear