How to connect a signal from class a to a slot in a class b?
Hello
i have this connection:
Code:
//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?
Code:
//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!
Re: How to connect a signal from class a to a slot in a class b?
you must use operator new for creating pointer to an object, so it should look like this
Code:
AClass *aClass = new AClass();
...
Re: How to connect a signal from class a to a slot in a class b?
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..
Re: How to connect a signal from class a to a slot in a class b?
did you add Q_OBJECT macro to both classes?
Re: How to connect a signal from class a to a slot in a class b?
Re: How to connect a signal from class a to a slot in a class b?
ok. can you prepare compilable example and post it? (if yes, can you put it in archive)
1 Attachment(s)
Re: How to connect a signal from class a to a slot in a class b?
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
Re: How to connect a signal from class a to a slot in a class b?
so, the problem is in ctor of Form2, you create a new instance of Form
Code:
...
Form *frm1=new Form;
...
but don't show it.
so, the right code is
Code:
...
Form *frm1=new Form;
frm1->show();
...
but, I strongly suggest you to review your code and make it more clearer.
Re: How to connect a signal from class a to a slot in a class b?
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
Re: How to connect a signal from class a to a slot in a class b?
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() ) );
Re: How to connect a signal from class a to a slot in a class b?
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?
Re: How to connect a signal from class a to a slot in a class b?
Then write the same function in Form2 class.
What you ask is not possible. Its like 2 classes sharing same function !! Is that possible ??
Re: How to connect a signal from class a to a slot in a class b?
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!