View Full Version : How to connect a signal from class a to a slot in a class b?
mekos
17th September 2008, 20:15
Hello
i have this connection:
//Class A
connect( treeWidgetA, SIGNAL( clicked( const QModelIndex& ) ), this, SLOT( doSomething(const QModelIndex& ) ) );
I have to move the doSomething slot in B class too, and i want to connect the B's class treeWidget signal clicked, to the slot doSomething that already exists in class A.is it correct?
//Class B
AClass *aClass;//is it correct?
connect( treeWidgetB, SIGNAL( clicked( const QModelIndex& ) ),aClass , SLOT( doSomething(const QModelIndex& ) ) );
This approach is not working..am a declaring Aclass wrong in the B's class code?
*the slot doSomething is declared as public slot in class A*
Thanks!
spirit
17th September 2008, 20:28
you must use operator new for creating pointer to an object, so it should look like this
AClass *aClass = new AClass();
...
mekos
17th September 2008, 20:45
i've tryed that too.. (declared the aClass within B's Class constructor).Never goes into the slot in the class A..however there are no complaints in the "compiler such as QObject::connect:No such slot etc etc"..just never gets into the slot to execute the code..can't undertand whats going on..
spirit
17th September 2008, 20:48
did you add Q_OBJECT macro to both classes?
mekos
17th September 2008, 20:50
yes in both headers..
spirit
17th September 2008, 20:54
ok. can you prepare compilable example and post it? (if yes, can you put it in archive)
mekos
17th September 2008, 21:31
ok did it..
i have a slot in the first form which writes text to the editbox and i just want to use the same slot in form2 to change the editbox in form2
spirit
17th September 2008, 21:46
so, the problem is in ctor of Form2, you create a new instance of Form
...
Form *frm1=new Form;
...
but don't show it.
so, the right code is
...
Form *frm1=new Form;
frm1->show();
...
but, I strongly suggest you to review your code and make it more clearer.
mekos
17th September 2008, 23:35
no the problem is not the form.The second form shows up correct..The problem is with the slot testSLot() implemented in Form.cpp, that i want to use it and in form2.cpp
aamer4yu
18th September 2008, 05:39
I guess u want that from Form2 pushbutton, u want to send a signal to the parent form.
Am i right ?
In that case u will have to connect the signal slot in OpenForm2() function.
connect ( frm->pushButton, SIGNAL ( clicked() ), this, SLOT ( testSlot() ) );
mekos
18th September 2008, 11:26
That's right..now when i click the button in form2 the slot sets the text in form1..however i need a way to use the testSlot to set the text in form2 when i click form2's pushbutton and not form1.Basically i need one slot to do the same thing in two different forms..Is that possible since the components in the 2 forms have the same names?
aamer4yu
18th September 2008, 12:41
Then write the same function in Form2 class.
What you ask is not possible. Its like 2 classes sharing same function !! Is that possible ??
mekos
18th September 2008, 15:04
Thats the way i had it..i had 4 functions in A class and exactly the same functions in class B.But with this i had 500 lines of code in class A repeated in class B and i was wondering if there is a way to write the code in one class and use the same code in another class..so nevermind i'll write the same code in the other class too..
Thanks!
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.